muparserx
muparserx copied to clipboard
Odd matrix/vector behaviour
Please consider the following example:
#include "muparserx/mpParser.h"
using namespace mup;
int
main()
{
ParserX p(pckALL_NON_COMPLEX | pckMATRIX);
p.EnableAutoCreateVar(true);
try {
p.SetExpr("a = zeros(3,3)"); p.Eval();
var_maptype vmap = p.GetVar();
for (var_maptype::iterator item = vmap.begin(); item!=vmap.end(); ++item)
std::cout << item->first << "=" << (Variable&)(*(item->second)) << "\n";
} catch (mup::ParserError const &e) {
std::cout << "error: " << e.GetMsg() << std::endl;
}
}
This works and prints
a={{0, 0, 0}; {0, 0, 0}; {0, 0, 0}}
I'll change the example a bit now (always just replacing the line p.SetExpr("a = zeros(3,3)"); p.Eval();
with something else and leaving the rest unchanged) and explain why I find the current behaviour to be broken and/or confusing.
- The way the matrix is printed cannot be read back in:
p.SetExpr("a={{0, 0, 0}; {0, 0, 0}; {0, 0, 0}}"); p.Eval();
leads to error: Undefined token "; {0, 0, 0}; {0, 0, 0}}" found at position 12.
- The above suggests I should not be using semicolons -- maybe commas work? Let's try that:
p.SetExpr("a={{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}"); p.Eval();
Nope: error: Can't evaluate function/operator "Array constructor": Parameter 1 of function "Array constructor" is invalid.
- Okay, so let's try to construct such a matrix line by line:
p.SetExpr("a[0]={1, 2, 3}"); p.Eval();
p.SetExpr("a[1]={4, 5, 6}"); p.Eval();
Hm: a={1, {4, 5, 6}, 3}
... That's now what we wanted. Indeed, the [0]
is ignored on the first line: replacing a[0]={1,2,3}
with a={1,2,3}
would have given us the same result.
- So apparently we need to tell muparserx first that
a
should be a matrix. So let's first make it a matrix and then assign its rows:
p.SetExpr("a[0]=zeros(3,3)"); p.Eval();
p.SetExpr("a[1]={4, 5, 6}"); p.Eval();
Nope, doesn't work: error: Index operator dimension error.
-- no idea why.
So basically, I want to be able to populate a matrix, say 3x3 with 9 entries on a single line: without resorting to setting each a[i,j]
manually. Is that possible somehow?