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.Wed, 06 Jan 2021 15:25:48 +0100Flatten relationshttps://ask.sagemath.org/question/55139/flatten-relations/I have a program in which I have created a list of variables
A = list(var('A_%d' % i) for i in (0..N))
and a list of expressions B involving the variables such that the i th expression involves A0, ... , Aj where j=i-1. The idea is the the B[i] should give you the formula for Ai. I now want to flatten all the relations by substituting in a number B[0] for A0, and then using B[i] to substitute inductively. E.g.
Suppose
B = [1, 2 A0, A0 + A1^2, A1 * A2, .....]
Then I want this to represent
A0 = 1, A1 = 2 A0, A2 = A0 + A1^2, A3 = A1 * A2, .....
and I want to produce the list obtained by solving these relations, i.e.
1, 2, 5, 5, ......
How should I go about this? Keep in mind that number of variables, N, will be quite large. To be a little more precise, I would actually like to obtain a list of arbitrary precision real balls, i.e. I want to solve the relations numerically with error bounds.Wed, 06 Jan 2021 06:31:38 +0100https://ask.sagemath.org/question/55139/flatten-relations/Answer by tmonteil for <p>I have a program in which I have created a list of variables</p>
<p>A = list(var('A_%d' % i) for i in (0..N))</p>
<p>and a list of expressions B involving the variables such that the i th expression involves A0, ... , Aj where j=i-1. The idea is the the B[i] should give you the formula for Ai. I now want to flatten all the relations by substituting in a number B[0] for A0, and then using B[i] to substitute inductively. E.g.</p>
<p>Suppose</p>
<p>B = [1, 2 A0, A0 + A1^2, A1 * A2, .....]</p>
<p>Then I want this to represent</p>
<p>A0 = 1, A1 = 2 A0, A2 = A0 + A1^2, A3 = A1 * A2, .....</p>
<p>and I want to produce the list obtained by solving these relations, i.e.</p>
<p>1, 2, 5, 5, ......</p>
<p>How should I go about this? Keep in mind that number of variables, N, will be quite large. To be a little more precise, I would actually like to obtain a list of arbitrary precision real balls, i.e. I want to solve the relations numerically with error bounds.</p>
https://ask.sagemath.org/question/55139/flatten-relations/?answer=55142#post-id-55142You can do:
sage: A = list(var('A_%d' % i) for i in (0..3))
sage: A
[A_0, A_1, A_2, A_3]
sage: B = [1, 2 * A_0, A_0 + A_1^2, A_1 * A_2]
You can generically construct the list of equations as follows:
sage: [A[i] == b for i,b in enumerate(B)]
[A_0 == 1, A_1 == 2*A_0, A_2 == A_1^2 + A_0, A_3 == A_1*A_2]
And you can solve the system:
sage: solve([A[i] == b for i,b in enumerate(B)], A)
[[A_0 == 1, A_1 == 2, A_2 == 5, A_3 == 10]]
Wed, 06 Jan 2021 06:55:50 +0100https://ask.sagemath.org/question/55139/flatten-relations/?answer=55142#post-id-55142Comment by Sébastien for <p>You can do:</p>
<pre><code>sage: A = list(var('A_%d' % i) for i in (0..3))
sage: A
[A_0, A_1, A_2, A_3]
sage: B = [1, 2 * A_0, A_0 + A_1^2, A_1 * A_2]
</code></pre>
<p>You can generically construct the list of equations as follows:</p>
<pre><code>sage: [A[i] == b for i,b in enumerate(B)]
[A_0 == 1, A_1 == 2*A_0, A_2 == A_1^2 + A_0, A_3 == A_1*A_2]
</code></pre>
<p>And you can solve the system:</p>
<pre><code>sage: solve([A[i] == b for i,b in enumerate(B)], A)
[[A_0 == 1, A_1 == 2, A_2 == 5, A_3 == 10]]
</code></pre>
https://ask.sagemath.org/question/55139/flatten-relations/?comment=55146#post-id-55146Note that:
sage: A = SR.var('A', 4)
sage: A
(A0, A1, A2, A3)
for which the latex is ok (the underscore is added for you):
sage: latex(A)
\left(A_{0}, A_{1}, A_{2}, A_{3}\right)Wed, 06 Jan 2021 11:29:35 +0100https://ask.sagemath.org/question/55139/flatten-relations/?comment=55146#post-id-55146Comment by tristan31415 for <p>You can do:</p>
<pre><code>sage: A = list(var('A_%d' % i) for i in (0..3))
sage: A
[A_0, A_1, A_2, A_3]
sage: B = [1, 2 * A_0, A_0 + A_1^2, A_1 * A_2]
</code></pre>
<p>You can generically construct the list of equations as follows:</p>
<pre><code>sage: [A[i] == b for i,b in enumerate(B)]
[A_0 == 1, A_1 == 2*A_0, A_2 == A_1^2 + A_0, A_3 == A_1*A_2]
</code></pre>
<p>And you can solve the system:</p>
<pre><code>sage: solve([A[i] == b for i,b in enumerate(B)], A)
[[A_0 == 1, A_1 == 2, A_2 == 5, A_3 == 10]]
</code></pre>
https://ask.sagemath.org/question/55139/flatten-relations/?comment=55154#post-id-55154Thank you both! The suggestion of tmonteil works perfectly for solving the equations symbolically. Unfortunately, I have too many relations to solve symbolically and so I must solve them numerically. I would like error bounds and so I would like to use RBF. To be more concrete, imagine that I set
B = [RBF(pi), 2 * A_0, A_0 + A_1^2, A_1 * A_2]
How would I then solve the same problem? I would like all the solutions to be of type RBF with error bounds.
Of course I could solve all the symbolic equations first and then substitute in the RBF value at the end. This however would be extremely computationally intensive and so I want to substitute iteratively.
I am sorry I should have phrased the original problem this way.
Thank you Sebastien for the additional suggestionWed, 06 Jan 2021 14:50:26 +0100https://ask.sagemath.org/question/55139/flatten-relations/?comment=55154#post-id-55154Comment by tristan31415 for <p>You can do:</p>
<pre><code>sage: A = list(var('A_%d' % i) for i in (0..3))
sage: A
[A_0, A_1, A_2, A_3]
sage: B = [1, 2 * A_0, A_0 + A_1^2, A_1 * A_2]
</code></pre>
<p>You can generically construct the list of equations as follows:</p>
<pre><code>sage: [A[i] == b for i,b in enumerate(B)]
[A_0 == 1, A_1 == 2*A_0, A_2 == A_1^2 + A_0, A_3 == A_1*A_2]
</code></pre>
<p>And you can solve the system:</p>
<pre><code>sage: solve([A[i] == b for i,b in enumerate(B)], A)
[[A_0 == 1, A_1 == 2, A_2 == 5, A_3 == 10]]
</code></pre>
https://ask.sagemath.org/question/55139/flatten-relations/?comment=55157#post-id-55157OK, I think I figured it out myself. For reference, the solution to my problem is the following:
sols = {A_0 : RBF(pi)}
for i in (1..N):
solutions[A[i]] = B[i](solutions)Wed, 06 Jan 2021 15:25:48 +0100https://ask.sagemath.org/question/55139/flatten-relations/?comment=55157#post-id-55157