Ask Your Question

Revision history [back]

use solutions from a loop generated solve

I am trying to extract a list of 3-tuples which solve a system of equations. For the test I am using a simply equation whose solutions go from negative to positive around one, so

n, u = var('n, u')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
L = [0, 1/4, 1/2, 3/4, 1, 5/4, 6/4, 7/4, 2]
for c in L:
    print (c,sltns[0].rhs(),sltns[1].rhs())

does not work.

What I am trying to get is, for example, if c = 1/2, then (1/2, 2, -2] because if c = 1/2 then n==2 and u==-2. And so on for all the elements of the list. () () ... ()

If I omit defining sltns, but use the solve() directly, I get out of range errors when for some c, u goes from positive to negative or vice versa for n, and I use [0].rhs(). Isn't sltns[2] the way to select, for example, [d==4] in [a==2, b==3, d==4], while sltns[0] and sltns[1] select the first two? Is it limited to some range positive or negative?

My question is: 1. What is the correct way to code the calculation above? 2. How would I alternatively code the loop if I needed the () s to be tuples in a matrix [(),(), ..., ()] for further calculations or plotting?

use solutions from a loop generated solve

I am trying to extract a list of 3-tuples which solve a system of equations. For the test I am using a simply equation whose solutions go from negative to positive around one, so

n, u = var('n, u')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
L = [0, 1/4, 1/2, 3/4, 1, 5/4, 6/4, 7/4, 2]
for c in L:
    print (c,sltns[0].rhs(),sltns[1].rhs())

does not work.

What I am trying to get is, for example, if c = 1/2, then (1/2, 2, -2] because if c = 1/2 then n==2 and u==-2. And so on for all the elements of the list. list.

()
()
...
()

()

If I omit defining sltns, but use the solve() directly, I get out of range errors when for some c, u goes from positive to negative or vice versa for n, and I use [0].rhs(). Isn't sltns[2] the way to select, for example, [d==4] in [a==2, b==3, d==4], while sltns[0] and sltns[1] select the first two? Is it limited to some range positive or negative?

My question is: 1. What is the correct way to code the calculation above? 2. How would I alternatively code the loop if I needed the () s to be tuples in a matrix [(),(), ..., ()] for further calculations or plotting?

use numerical solutions from a loop generated solvefor-loop solve()

I am trying to extract a list of 3-tuples which solve a system of equations. For the test I am using a simply equation whose solutions go from negative to positive around one, so

n, u = var('n, u')
sltns=solve([n + u==0, n + c*u - 1==0], n, u)
L = [0, 1/4, 1/2, 3/4, 1, 5/4, 6/4, 7/4, 2]
for c in L:
    print (c,sltns[0].rhs(),sltns[1].rhs())

does not work.

What I am trying to get is, for example, if c = 1/2, then (1/2, 2, -2] because if c = 1/2 then n==2 and u==-2. And so on for all the elements of the list.

()
()
...
()

If I omit defining sltns, but use the solve() directly, I get out of range errors when for some c, u goes from positive to negative or vice versa for n, and I use [0].rhs(). Isn't sltns[2] the way to select, for example, [d==4] in [a==2, b==3, d==4], while sltns[0] and sltns[1] select the first two? Is it limited to some range positive or negative?

My question is: 1. What is the correct way to code the calculation above? 2. How would I alternatively code the loop if I needed the () s to be tuples in a matrix [(),(), ..., ()] for further calculations or plotting?