I need to evaluate many times the following function: f[x_,y_]:=x*A+y*B
, where x,y
are real numbers and A,B
are large NxN symmetric matrices of real numbers (e.g. 1000x1000). I am trying to optimize this function. Compiling the function does not seem to help. However, as the matrices are symmetric, it is redundant to sum all the $N^2$ terms; it would be enough to sum the relevant $N(N+1)/2$ terms. Does someone know how to efficiently do this?
UPDATE: As suggested by bill s, one could store the following flatten versions of the upper triangular parts:
Aup=DeleteCases[Flatten[UpperTriangularize[A]],0];
Bup=DeleteCases[Flatten[UpperTriangularize[A]],0];
and then do:
fup[x_,y_]:=x*Aup+y*Bup
.
Is there an efficient way to map the vector fup[x,y]
into a symmetric matrix?
I have found this thread but I cannot understand well R.
Developer`PackedArrayQ
. $\endgroup$UpperTriangularize
thing you need to runDeveloper`ToPackedArray
on the result. In the end its a lot of trouble to save less than a factor of two. $\endgroup$