How to access matrix elements of an indexed expressions
Hi, I want to use ginac to evaluate some complex expressions that evolve some vector algebra. However, have some trouble working with the matrix class. I have 2 questions: 1: Am I correct, that if i want to compute the Subtraction of two vectors I should use the indexed variant? 2: How do I access elements of an expression? I have included a small file that illustrates my problem? Kind regards, Spooky #include <ginac/ginac.h> #include <iostream> int main(int argc,char **argv) { using namespace GiNaC; idx d(symbol("d"),3); matrix m0 = matrix({{0,0,0}}), m1 = matrix({{1,0,0}}); indexed v0 = indexed(m0,d), v1 = indexed(m1,d); // This seems wrong: std::cout<< (m1-m0).eval()<< std::endl; // -> [[1,0,0]]-[[0,0,0]] // This seems right: std::cout<< (v1-v0).simplify_indexed()<< std::endl; // -> [[1,0,0]].d // I can normally access matrix element using std::cout<< m1(0,0) << std::endl; // -> 0 // But how can I access matrix elements of an expression? // does not compile: // std::cout << "(v1-v0)(0,0): " << (v1-v0)(0,0) << std::end; return 0; }
Hi! On 11.01.25 10:40, Bastian Abt via GiNaC-list wrote:
1: Am I correct, that if i want to compute the Subtraction of two vectors I should use the indexed variant?
Use evalm().
2: How do I access elements of an expression?
You could use numeric indices: #include <ginac/ginac.h> #include <iostream> int main(int argc, char **argv) { using namespace GiNaC; matrix m0 = matrix({{1, 2, 3}}), m1 = matrix({{6, 5, 4}}); std::cout << (m1 - m0).evalm() << std::endl; // -> [[5,3,1]] idx i1(1, 3); std::cout << indexed((m1 - m0).evalm(), i1) << std::endl; // -> 3 } Bye, Christian -- / Physics is an algorithm \/ www.cebix.net
Hi, thank you very much, this makes much more sense. And thank you very much to everyone who helped create GiNaC. It is wonderful fast and wonderful nice to use! Kind regards Spooky
participants (2)
-
Bastian Abt
-
Christian Bauer