Group Abstract Group Abstract

Message Boards Message Boards

0
|
23 Views
|
0 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to set part of a Product-Field-Value that is an Array?

I have recently dug into the manual for the capabilites of FunctionCompile in the Wolfram Language.
One addition that has peaked my interest is the "Product" TypeDeclaration with "Serializable" AbstractType.

From my understanding, this Type-Declaration allows
(1) for the creation of a Datastructures of fixed type to be passed to compiled functions and
(2) compiled functions to alter the values of fields even though they are given as arguments.

I intent to make use of this functionality by compiling routines that take multiple lists, counters and booleans as arguments and alter them depending on the internal outcome.
However, FunctionCompile returns an error messages when I try to set part of the field value in FunctionCompile.
Here is a small example for the declaration of such a DataStructure and a function that attempts to change a single list entry.

decl = TypeDeclaration["Product", "MolecPositions",
    <|"vec" -> "PackedArray"::["Real64",2]|>,
    "AbstractTypes" -> "DefaultSerializable"
];
Translation=FunctionCompile[decl,
    Function[{Typed[vec,"MolecPositions"]},
       vec["vec"][[1,1]] += RandomReal[{0,1}];
    ]
]

FunctionCompile::err: Error. Cannot assign a part to an immutable object

From the documentation of "TypeDeclaration" I know that field values can be changed due to the methods defined for objects of the "Product" type. My best attempt to fix the issue above is to create a local copy of the field value with block, in which i can set parts, and then pass the altered version of the field value back.

Translation=FunctionCompile[decl,
    Function[{Typed[vec,"MolecPositions"]},
       Block[{vecTmp},
         vecTmp=vec["vec"];
         vecTmp[[1,1]]+=RandomReal[{0,1}];
         vec["vec"]=vecTmp;
       ]
    ]
];

This works for a "PackedArray", but fails if "vec" is of type "ListVector":["PackedArray"::["Real64",1].

decl=TypeDeclaration["Product","MolecPositions",
    <|"vec"->"ListVector"::["PackedArray"::["Real64",1]]|>,
    "AbstractTypes"->"DefaultSerializable"
];
Translation=FunctionCompile[decl,
    Function[{Typed[vec,"MolecPositions"]},
       Block[{vecTmp},
         vecTmp=vec["vec"];
         vecTmp[[1,1]]+=RandomReal[{0,1}];
         vec["vec"]=vecTmp;
       ]
    ]
];

FunctionCompile::err: Cannot find a definition for the function Native`GetPartBinary that takes arguments with the types ListVector[PackedArray[Real64, 1:Integer64]], Integer64 and Integer64.

Questions:

Are there any suggestions on how to fix my issue with array field types?
Is there an alternative implementation for this kind of use case?
Will the functionality of 'Native`SetBinary' be extended for "ListVector"?
Is there a different way to set part of a array field value?
Should I switch to a different TypeDelcaration if the product only has one field value?

POSTED BY: Michael Haring
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard