ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 20 Jul 2024 20:28:16 +0200convert symbolic matrix to numeric onehttps://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/Being new to sagemath I have the following problem:
I have a rather large symbolic matrix M that depends on a collection of variables.
the variables are collected in a list like this
sage: v = [var('val_%a' %i) for i in range(5) ]
sage: v
(val_0, val_1, val_2, val_3, val_4)
The Matrix M depends on some complicated way on the variables val_0 , val_1 etc...
Now I want to evaluate M for some particular values val[i] which are numbers
what I do is ceate a dictionary
sage: subsdict={ v[i]:i for i in range(5)}
and then substitute the values in M
sage: Mnumeric = M.subs(subsdict)
my problem is that this substitution is very slow
(of course in the real problem its not only 5 variables but rather 100)Sat, 20 Jul 2024 13:42:47 +0200https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/Comment by vdelecroix for <p>Being new to sagemath I have the following problem:
I have a rather large symbolic matrix M that depends on a collection of variables.
the variables are collected in a list like this</p>
<pre><code>sage: v = [var('val_%a' %i) for i in range(5) ]
sage: v
(val_0, val_1, val_2, val_3, val_4)
</code></pre>
<p>The Matrix M depends on some complicated way on the variables val_0 , val_1 etc...
Now I want to evaluate M for some particular values val[i] which are numbers
what I do is ceate a dictionary</p>
<pre><code>sage: subsdict={ v[i]:i for i in range(5)}
</code></pre>
<p>and then substitute the values in M</p>
<pre><code>sage: Mnumeric = M.subs(subsdict)
</code></pre>
<p>my problem is that this substitution is very slow
(of course in the real problem its not only 5 variables but rather 100)</p>
https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78341#post-id-78341`RDF` would be machine floating point numbers. That should give you the fastest arithmetic. But you could also use
- `RealField`: arbitrary precision floating point numbers (using mpfr in the background)
- `RealBallField`: real balls (using arb in the background)Sat, 20 Jul 2024 20:28:16 +0200https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78341#post-id-78341Comment by Karl007 for <p>Being new to sagemath I have the following problem:
I have a rather large symbolic matrix M that depends on a collection of variables.
the variables are collected in a list like this</p>
<pre><code>sage: v = [var('val_%a' %i) for i in range(5) ]
sage: v
(val_0, val_1, val_2, val_3, val_4)
</code></pre>
<p>The Matrix M depends on some complicated way on the variables val_0 , val_1 etc...
Now I want to evaluate M for some particular values val[i] which are numbers
what I do is ceate a dictionary</p>
<pre><code>sage: subsdict={ v[i]:i for i in range(5)}
</code></pre>
<p>and then substitute the values in M</p>
<pre><code>sage: Mnumeric = M.subs(subsdict)
</code></pre>
<p>my problem is that this substitution is very slow
(of course in the real problem its not only 5 variables but rather 100)</p>
https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78340#post-id-78340Thanks for the quick answer. Yes, I guess I could construct the matrix as a numerical matrix instead of a symbolic first. I thought I could somehow do somthing similar as in Mathematica where the following works (and is fast)
`In[1]:= M={{a,b},{c,d}}`
`In[3]:= a=1.5 `
`In[4]:= M`
`Out[4]= {{1.5, b}, {c, d}}`
In the end I would like to end up with a matrix of floats (I guess that's RDF in Sagemath?)Sat, 20 Jul 2024 18:42:30 +0200https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78340#post-id-78340Comment by Emmanuel Charpentier for <p>Being new to sagemath I have the following problem:
I have a rather large symbolic matrix M that depends on a collection of variables.
the variables are collected in a list like this</p>
<pre><code>sage: v = [var('val_%a' %i) for i in range(5) ]
sage: v
(val_0, val_1, val_2, val_3, val_4)
</code></pre>
<p>The Matrix M depends on some complicated way on the variables val_0 , val_1 etc...
Now I want to evaluate M for some particular values val[i] which are numbers
what I do is ceate a dictionary</p>
<pre><code>sage: subsdict={ v[i]:i for i in range(5)}
</code></pre>
<p>and then substitute the values in M</p>
<pre><code>sage: Mnumeric = M.subs(subsdict)
</code></pre>
<p>my problem is that this substitution is very slow
(of course in the real problem its not only 5 variables but rather 100)</p>
https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78339#post-id-78339Complemlent to @vdelecroix's important remark :
> ... I have a rather large symbolic matrix M...
Isn't that the source of the problem ? Basically, what you want to do is necessarily equivalent to
matrix(SR, [[u.subs(subsdict) for u in v] for v in M])s
Therefore, you are doing `M.nrows()*M.ncols()` substitutions.
Ancilliary question : what is the desired type (or parent) of the elements of the substituted matrix ? According to your problem, it might be wise to convert the matrix elements
- to QQ, AA, QQbar (if your elements are all rational, algebraix real or algebraic respectively), or
- to RDF or CDF (if what you want to do with this matrix is essentially numeric).
Or possibly to the relevant Interval or Ball field (if you have to account for result precision).Sat, 20 Jul 2024 16:22:52 +0200https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78339#post-id-78339Comment by vdelecroix for <p>Being new to sagemath I have the following problem:
I have a rather large symbolic matrix M that depends on a collection of variables.
the variables are collected in a list like this</p>
<pre><code>sage: v = [var('val_%a' %i) for i in range(5) ]
sage: v
(val_0, val_1, val_2, val_3, val_4)
</code></pre>
<p>The Matrix M depends on some complicated way on the variables val_0 , val_1 etc...
Now I want to evaluate M for some particular values val[i] which are numbers
what I do is ceate a dictionary</p>
<pre><code>sage: subsdict={ v[i]:i for i in range(5)}
</code></pre>
<p>and then substitute the values in M</p>
<pre><code>sage: Mnumeric = M.subs(subsdict)
</code></pre>
<p>my problem is that this substitution is very slow
(of course in the real problem its not only 5 variables but rather 100)</p>
https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78338#post-id-78338Why not substitute before creating the matrix?Sat, 20 Jul 2024 14:04:37 +0200https://ask.sagemath.org/question/78337/convert-symbolic-matrix-to-numeric-one/?comment=78338#post-id-78338