使用rpy2从FloatVector减去FloatVector

时间:2018-11-22 22:38:41

标签: rpy2

在rpy2的帮助下,我在Python代码中的R中使用grf包。但是,我对rpy2返回的结构有些困惑。

在这里,我试图简单地求出两个向量的差。如果它只是R中的向量,我将简单地使用-减去。

但这是我的问题:

// Helper function to retrieve (or create) constraint group
internal static ObjectId getConstraintGroup(bool createIfDoesNotExist)
{
    // Calculate the current plane on which new entities are added by the editor
    // (A combination of UCS and ELEVATION sysvar).
    Editor editor = Application.DocumentManager.MdiActiveDocument.Editor;
    Matrix3d ucsMatrix = editor.CurrentUserCoordinateSystem;
    Point3d origin = ucsMatrix.CoordinateSystem3d.Origin;
    Vector3d xAxis = ucsMatrix.CoordinateSystem3d.Xaxis;
    Vector3d yAxis = ucsMatrix.CoordinateSystem3d.Yaxis;
    Vector3d zAxis = ucsMatrix.CoordinateSystem3d.Zaxis;
    origin = origin + Convert.ToDouble(Application.GetSystemVariable("ELEVATION")) * zAxis;
    Plane currentPlane = new Plane(origin, xAxis, yAxis);

    // get the constraint group from block table record
    ObjectId ret = ObjectId.Null;
    Database database = HostApplicationServices.WorkingDatabase;
    ObjectId networkId = AssocNetwork.GetInstanceFromObject(SymbolUtilityServices.GetBlockModelSpaceId(database), createIfDoesNotExist, true, "");
    if (!networkId.IsNull)
    {
        // Try to find the constraint group in the associative network
        using (Transaction transaction = database.TransactionManager.StartTransaction())
        {
            using (AssocNetwork network = transaction.GetObject(networkId, OpenMode.ForRead, false) as AssocNetwork)
            {
                if (network != null)
                {
                    // Iterate all actions in network to find Assoc2dConstraintGroups
                    ObjectIdCollection actionsInNetwork = network.GetActions;
                    for (int nCount = 0; nCount <= actionsInNetwork.Count - 1; nCount++)
                    {
                        ObjectId idAction = actionsInNetwork[nCount];
                        if (idAction == ObjectId.Null)
                        {
                            continue;
                        }

                        // Is this action a type of Assoc2dConstraintGroup?
                        if (idAction.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Assoc2dConstraintGroup))))
                        {
                            using (AssocAction action = (AssocAction)transaction.GetObject(idAction, OpenMode.ForRead, false))
                            { 
                                if (action == null)
                                {
                                    continue;
                                }

                                Assoc2dConstraintGroup constGrp = action as Assoc2dConstraintGroup;
                                // Is this the Assoc2dConstraintGroup for our plane of interest?
                                if (constGrp.WorkPlane.IsCoplanarTo(currentPlane))
                                {
                                    // If it is then we've found an existing constraint group we can use.
                                    ret = idAction;
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            // If we get to here, a suitable contraint group doesn't exist, create a new one if that's what calling fn wanted.
            if (ret.IsNull && createIfDoesNotExist)
            {
                using (AssocNetwork network = (AssocNetwork)transaction.GetObject(networkId, OpenMode.ForWrite, false))
                {
                    // Create  construction plane
                    Plane constraintPlane = new Plane(currentPlane);
                    // If model extent is far far away from origin then we need to shift
                    // construction plane origin within the model extent.
                    // (Use Pextmin, PExtmax in paper space)
                    Point3d extmin = database.Extmin;
                    Point3d extmax = database.Extmax;
                    if (extmin.GetAsVector().Length > 100000000.0)
                    {
                        Point3d originL = extmin + (extmax - extmin) / 2.0;
                        PointOnSurface result = currentPlane.GetClosestPointTo(originL);
                        constraintPlane.Set(result.GetPoint(), currentPlane.Normal);
                    }
                    // Create the new constraint group and add it to the associative network.
                    using (Assoc2dConstraintGroup constGrp = new Assoc2dConstraintGroup(constraintPlane))
                    {
                        ret = database.AddDBObject(constGrp);
                    }
                    network.AddAction(ret, true);
                }
            }
            transaction.Commit();
        }
    }
    return ret;
}

现在,当我尝试像这样减去时:

    pred_w1.rx2("predictions")
    Out[92]: 
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x1c17447e08 / R:0x7fe214bf7c00>
   [-0.548409, -0.224735, 0.948285, 0.269825, ..., 0.259327, -0.267804, 
    0.255273, 0.287592]


    pred_w1.rx2("predictions")
    Out[93]: 
    R object with classes: ('numeric',) mapped to:
    <FloatVector - Python:0x1c18f02808 / R:0x7fe214bf7c00>
    [-0.548409, -0.224735, 0.948285, 0.269825, ..., 0.259327, 
    -0.267804, 0.255273, 0.287592]

我收到此错误:

    pred_w1.rx2("predictions") - pred_w0.rx2("predictions")`

我认为这应该很容易。我只是不太熟悉rpy2。

1 个答案:

答案 0 :(得分:0)

使用委托人ro(如 r o 执行者):

pred_w1.rx2("predictions").ro - pred_w0.rx2("predictions")

(文档中的更多信息:https://rpy2.github.io/doc/v2.9.x/html/vector.html#operators