# [GiNaC-list] Bug with matrix subtraction

Sheplyakov Alexei varg at theor.jinr.ru
Thu Mar 22 10:57:46 CET 2007

```Hello!

On Thu, Mar 22, 2007 at 09:44:03AM +0100, Martin Sandve Alnæs wrote:

> If I subtract two equal matrices, I get the scalar 0.

In GiNaC (A-A) is always transformed into 0 (number). I admit this is
ugly, but fixing it would be very difficult (if possible at all).

> This messes up later calculations, since a scalar doesn't have op(i),

numeric::nops() returns zero, so loops like this

for (size_t i = 0; i < e.nops(); ++i) {
// do something
}

should work fine.

> transpose(), etc.

You have to explicitly check for .is_zero().

> If I do a+(-b) instead of a-b, I get a zero matrix as wanted.

Interesting. I get zero (a number) in both ways:

\$ cat test_matr_minus.cpp
#include <iostream>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;

int main(int argc, char** argv) {
matrix a(2, 2);
a = 1, 2,
3, 4;
cout << "a = " << a << endl;
ex test = a - a;
cout << "a - a = " << test << endl;
ex test2 = a + (-a);
cout << "a + (-a) = " << test2 << endl;
return 0;
}

\$ g++ test_matr_minus.cpp -lginac
\$ ./a.out
a = [[1,2],[3,4]]
a - a = 0
a + (-a) = 0

Best regards,
Alexei

--
All science is either physics or stamp collecting.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 827 bytes
Desc: Digital signature
Url : http://www.cebix.net/pipermail/ginac-list/attachments/20070322/cc9db272/attachment.pgp
```