[GiNaC-list] write access to entries of submatrices [Was: multi index storage of expressions]

Sheplyakov Alexei varg at theor.jinr.ru
Tue Feb 20 07:52:16 CET 2007


Hello,

On Mon, Feb 19, 2007 at 08:56:30PM -0300, Charlls Quarra wrote:

> I just noted that the following write access attempt
> will fail:
> 
> 	matrix foo(1 , 2);
> 	for (int i=0 ; i< 2 ; i++)
> 	{
> 		foo(0,i) = matrix(2,2);
> 	}
> 	ex_to<matrix>( foo(0,1) )(1,1) = 34;
> 	cout << "it was wrote -> " << ex_to<matrix>( foo( 0 ,
> 1 ) )(1,1) << endl;
 
[error message snipped]

ex_to<T> returns reference to _constant_ object (otherwise reference
count would be broken), so you can't assign anything to such object.

> any idea how to write to the entries of a submatrix in
> this way?

There is no way to do this (leaving aside evil hacks).

Back to the original task:

> i would also want to make computations with an object like
> D( f_{i} ) / dx_{j} dx_{k}

You don't have to use some GiNaC class to represent such objects, e.g.

#include <iostream>
#include <vector>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;

int main(int argc, char** argv)
{
	symbol x("x"), y("y"), u("u"), v("v"), w("w"), a("a"), b("b"), c("c");
	std::vector<symbol> X(2);
	X[0] = x;
	X[1] = y;
	std::vector<ex> F(2);
	F[0] = a*x + b*y + c*x*y;
	F[1] = u*pow(x,2) + v*pow(y,2) + w*x;
	std::vector<matrix> D2F(2);
	for (size_t i=0; i < 2; i++) {
		D2F[i] = matrix(2, 2);
		for (size_t j=0; j < 2; j++)
			for (size_t k=0; k <= j; k++) {
				D2F[i](j, k) = F[i].diff(X[j]).diff(X[k]);
				D2F[i](k, j) = D2F[i](k, j); // suppose functions are smooth enough
			}
	}
	for (size_t i=0; i<2; i++)
		cout << "D^2(" << F[i] << ") = "  << D2F[i] << endl;

	return 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/20070220/368db2ac/attachment.pgp


More information about the GiNaC-list mailing list