Ask Your Question
2

Flatten relations

asked 2021-01-06 06:31:38 +0100

tristan31415 gravatar image

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.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2021-01-06 06:55:50 +0100

tmonteil gravatar image

You 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]]
edit flag offensive delete link more

Comments

1

Note 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)
Sébastien gravatar imageSébastien ( 2021-01-06 11:29:35 +0100 )edit

Thank 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 suggestion

tristan31415 gravatar imagetristan31415 ( 2021-01-06 14:50:26 +0100 )edit

OK, 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)
tristan31415 gravatar imagetristan31415 ( 2021-01-06 15:25:48 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2021-01-06 06:31:38 +0100

Seen: 801 times

Last updated: Jan 06 '21