Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacement rules for large matrixes

54 views
Skip to first unread message

Gabriel Landi

unread,
Jun 24, 2011, 7:54:37 AM6/24/11
to
Greetings everyone.

In some simulations I have been doing with Mathematica, I first set up a
large symbolic sparse matrix (4000 x 4000).
I don't have a simple set of array rules to do this. The matrix is generated
by a somewhat complicated symbolic pattern matching. This takes around 5-10
seconds.

The point is that, once it is set up I replace the symbolic values with
different numerical values and compute quantities related to Eigenvalues and
LinearSolve.

My problem is that the replacement from symbolic to numeric also takes
around 5 to 10 seconds.
I was wondering if you guys had any advice on how to speed this up. I tried
using Dispatch but it doesn't make a difference because the number of
symbolic values to replace is small (usually 6).

So my question is: starting with a sparse symbolic matrix dependent on a
small number of parameters, what is the fastest way to generate a numerical
matrix from it?

Thanks in advance,

Gabriel


dr DanW

unread,
Jun 25, 2011, 5:27:46 AM6/25/11
to
Is your matrix a nested list or a SparseArray[]? From your description, I am guessing it is a nested list. Try working with your matrix as a SparseArray[] instead.

Next problem: Replace[] does not work on SparseArray[]'s. However, you can use BlockRules[] (code below) to do the replacement.

Code for BlockRules[] by Andrew Moylen at Wolfram, from an earlier posting on this group that I can't find right now:

SetAttributes[BlockRules, HoldRest];

BlockRules[rules_, expr_] := Block @@ Append[Apply[Set, Hold[rules], {2}],
Unevaluated[expr]]

Enjoy,
Daniel

Gabriel Landi

unread,
Jun 26, 2011, 6:29:30 AM6/26/11
to
Dear Daniel,

Thank you for the reply. My matrix is constructed from a system of
polynomial equations using CoefficientArrays.
Indeed, replacement does not work. What I was doing was (sadly)

SparseArray[Normal@matrix /.rules].

Gabriel

Gabriel Landi

unread,
Jun 26, 2011, 5:38:27 PM6/26/11
to
0 new messages