Group Abstract Group Abstract

Message Boards Message Boards

Mathematica++ A C++ Library with Sugar

Posted 7 years ago
POSTED BY: Neel Basu
17 Replies
POSTED BY: Neel Basu
POSTED BY: Neel Basu

I was working on switching between ML and WS.

That's great! I'll try it out on the weekend :-)

POSTED BY: Szabolcs Horvát

I just pushed some changes to use The PutArray functions is the input is a nesting of std::vector<std::vector<...>> and that vector forms a matrix or higher dimensional matrix or tensor of proper shape.

typedef std::vector<std::vector<std::vector<int>>> ivv_type; // 2 x 2 x 3
ivv_type matrix;
std::vector<int> row11 = {111, 112, 113};
std::vector<int> row12 = {121, 122, 123};
std::vector<int> row21 = {211, 212, 213};
std::vector<int> row22 = {221, 222, 223};

std::vector<std::vector<int>> row1;
std::vector<std::vector<int>> row2;

row1.push_back(row11);
row1.push_back(row12);

row2.push_back(row21);
row2.push_back(row22);

matrix.push_back(row1);
matrix.push_back(row2);

value res;

shell.enable(mathematica::bulk_io); // ENABLE this feature
shell << Total(Flatten(matrix));
shell >> res;

BOOST_CHECK(*res == 2004);

The above will use MLPutInteger32Array/ WSPutInteger32Array

These two changes are currently in develop branch. I'll have some more tests and then I'll put them in master branch.

After that I'll use Armadillo Eigen etc.. as optional dependencies.

POSTED BY: Neel Basu
POSTED BY: Szabolcs Horvát

I can detect a packed array but I cannot detect whether all elements in that are are int64 or int32 or double or string because List is not uniform. So I wrap it in a variant type and fetch one by one.

POSTED BY: Neel Basu

I can detect a packed array but I cannot detect whether all elements in that are are int64 or int32 or double or string because List is not uniform.

Do you mean that you can detect a List expression (not a packed array)?

A packed array is always homogeneous. All elements will have the same numeric type (no strings allowed). See the link I posted (chatroom discussion) on how to detect not a generic List, but specifically a packed array, and how to determine the type of its elements.

Also take a look here: https://mathematica.stackexchange.com/questions/3496/what-is-a-mathematica-packed-array

A packed array is essentially what you might call a C array---a contiguous block of numerical data (e.g. doubles). It is the storage format that is the most efficient as well as the easiest to handle in C (or C++). In most scientific applications that I came across, it is the single most useful data structure that one might want to exchange with Mathematica. This is also why LibraryLink supports it directly (see MTensor).

POSTED BY: Szabolcs Horvát

Aha I see. Yes I was talking about detecting a List. I'll have a look into packed array

POSTED BY: Neel Basu

I was working on switching between ML and WS. Now -DWITH_ML=ON can be set on cmake to use MathLink instead of using WSTP. I just pushed the changes into develop branch. Things are working as expected.

POSTED BY: Neel Basu
POSTED BY: Neel Basu

I should have spelt out that I am very interested in using this library if it can be made compatible with LibraryLink, and it there are workarounds for potential performance issues (e.g. use a PutReal64Array manually when advantageous, even as part of a larger expression).

With LibraryLink one is basically given an MLINK (WSLINK) object and then uses it to read off the arguments passed to the corresponding Mathematica function, then writes the return value to it.

The limitation is that LibraryLink still only works with mathlink.h. It is possible that it can be made to work with wstp.h, but this would not be straightforward and so far I haven't tried very hard (as I would not have gotten anything in return--it would only have made compilation more difficult).

POSTED BY: Szabolcs Horvát
POSTED BY: Neel Basu
POSTED BY: Neel Basu
POSTED BY: Szabolcs Horvát
POSTED BY: Szabolcs Horvát

Thank you for the very first review. I am glad to know that it worked nicely.

POSTED BY: Neel Basu

That's exactly what I was hoping/looking for, before I decided to also learn the Wolfram Language!! Absolutely amazing, thanks for your hard, fruit-bearing work which I know from gitlab. I've tested some other sample codes and so far so good, everything works like a charm, fantastic!!

Mathematica++

Simply brilliant!!!

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