Solving of a linear system with parameters - linear-algebra

I have a linear system of four equations with four variables $a,b,c,d$ and two parameters $i,h$ where equations are roughly of the form
$$a h^3 i^3 + b h^2 i^2 +c h i +d=0$$
I want to get $a,b,c,d$ in terms of $i,h$.
Is this possible in SymPy? If not, can someone recomend how to do it on some other way?

For completeness, the answer is yes, solve in Sympy solves systems of equations with parameters. An example using the equation you stated:
from sympy import *
var('a b c d i h')
eqns = [a*h**3*i**3 + b*h**2*i**2 + c*h*i + d, a+b+c+d, a-b*h*i**2 -c - d, a+b+c-h**2 - i**2]
solve(eqns, [a,b,c,d])
The first argument of solve is a list of equations, the second the list of variables to solve for. The output is a solution, presented as a dictionary:
{c: (h**2 + i**2)*(-h**4*i**5 + h**3*i**3 - 2*h**2*i**2 + h*i**2 + 1)/(h*i*(-h**3*i**4 + h**2*i**2 + h*i**2 - 2*h*i + 1)),
b: -(2*h**2 + 2*i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
a: (-h**3*i**2 + h**2 - h*i**4 + i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
d: -h**2 - i**2}

Related

Solve multi-objective optimization problem with R package rmoo

The documentation to this packages is available here, but for me it is impossible to transfer my real-valued 3 objective function to a form that it works.
I'm trying to solve it with the nsga2 function - and I think nsga3 should work the same way?!
My objective functions, which I want to minimize, are:
f1 <- 1640.2823 + 2.3573285*x[1] + 2.3220035*x[2] +4.5688768*x[3] + 7.7213633*x[4] + 4.4559504*x[5]
f2 <- 6.5856 + 1.15*x[1] - 1.0427*x[2] + 0.9738*x[3] + 0.8364*x[4] - 0.3695*x[1]*x[4] + 0.0861*x[1]*x[5] + 0.3628*x[2]*x[4] - 0.1106*x[1]^2 - 0.3437*x[3]^2 + 0.1764*x[4]^2
f3 <- -0.0551 + 0.0181*x[1] + 0.1024*x[2] + 0.0421*x[3] - 0.0073*x[1]*x[2] + 0.024*x[2]*x[3] - 0.0118*x[2]*x[4] - 0.0204*x[3]*x[4] - 0.008*x[3]*x[5] - 0.0241*x[2]^2 + 0.0109*x[4]^2
And x has to be between one and three (these are the constraints)
1 <= x[i] <= 3
I appreciate any hints... so many thanks in advance!

Solve recurrence: T(n) = T(n − 1) + T(n − 2) + 3

T(1) = T(2) = 1, and for n > 2, T(n) = T(n − 1) + T(n − 2) + 3.
What Ive done so far:
T(n-1) = T(n-2) + T(n-3) + 3 + 3
T(n-2) = T(n-3) + T(n-4) + 3 + 3 + 3
T(n) = T(n-2) + 2T(n-3) + T(n-4) + 3 + 3 + 3 + 3 + 3
T(n) = T(1) + 2T(2) + T(n-4) + 3(n + 2)
Im not sure if this is right, and if it is, how do I get rid of T(n-4).
These types of recurrences are tricky, and the repeated expansion method will unfortunately get you nowhere. Observing the recursion tree will only give you an upper bound, which is often not tight.
Two methods I can suggest:
1. Substitution + Standard Theorem
Make the following variable substitution:
This is in the correct form for the Akra-Bazzi method, with parameters:
2. Fibonacci formula
The Fibonacci series has an explicit formula which can be derived by guessing a solution of the form Fn = a^n. Using this as an analogy, substitute a similar expression for T(n):
Equating the constant and exponential terms:
Take the positive root because the negative root has absolute value less than 1, and will therefore decay to zero with increasing n:
Which is consistent with (1).

Solving the recurrence equation T(n) = 3 + m * T(n - m)

I have a Computer Science Midterm tomorrow and I need help determining the complexity of a particular recursive function as below, which is much complicated than the stuffs I've already worked on: it has two variables
T(n) = 3 + mT(n-m)
In simpler cases where m is a constant, the formula can be easily obtained by writing unpacking the relation; however, in this case, unpacking doesn't make the life easier as follows (let's say T(0) = c):
T(n) = 3 + mT(n-m)
T(n-1) = 3 + mT(n-m-1)
T(n-2) = 3 + mT(n-m-2)
...
Obviously, there's no straightforward elimination according to these inequalities. So, I'm wondering whether or not I should use another technique for such cases.
Don't worry about m - this is just a constant parameter. However you're unrolling your recursion incorrectly. Each step of unrolling involves three operations:
Taking value of T with argument value, which is m less
Multiplying it by m
Adding constant 3
So, it will look like this:
T(n) = m * T(n - m) + 3 = (Step 1)
= m * (m * T(n - 2*m) + 3) + 3 = (Step 2)
= m * (m * (m * T(n - 3*m) + 3) + 3) + 3 = ... (Step 3)
and so on. Unrolling T(n) up to step k will be given by following formula:
T(n) = m^k * T(n - k*m) + 3 * (1 + m + m^2 + m^3 + ... + m^(k-1))
Now you set n - k*m = 0 to use the initial condition T(0) and get:
k = n / m
Now you need to use a formula for the sum of geometric progression - and finally you'll get a closed formula for the T(n) (I'm leaving that final step to you).

Defining a particular polynomial ring in some CAS (Computer Algebra System)

I'm interested in defining the following polynomial quotient ring in some CAS (Singular, GAP, Sage, etc.):
R = GF(256)[x] / (x^4 + 1)
Specifically, R is the set of all polynomials of degree at most 3, whose coefficients belong to GF(256). Two examples include:
p(x) = {03}x^3 + {01}x^2 + {01}x + {02}
q(x) = {0B}x^3 + {0D}x^2 + {09}x + {0E}
Addition and multiplication are defined as the per ring laws. Here, I mention them for emphasis:
Addition: The corresponding coefficients are XOR-ed (the addition law in GF(256)):
p(x) + q(x) = {08}x^3 + {0C}x^2 + {08}x + {0C}
Multiplication: The polynomials are multiplicated (coefficients are added and multiplicated in GF(256)). The result is computed modulo x^4 + 1:
p(x) * q(x) = ({03}*{0B}x^6 + ... + {02}*{0E}) mod (x^4 + 1)
= ({03}*{0B}x^6 + ... + {02}*{0E}) mod (x^4 + 1)
= ({1D}x^6 + {1C}x^5 + {1D}x^4 + {00}x^3 + {1D}x^2 + {1C}x + {1C}) mod (x^4 + 1)
= {01}
Please tell me how to define R = GF(256)[x] / (x^4 + 1) in a CAS of your choice, and show how to implement the above addition and multiplication between p(x) and q(x).

solving equations and using values obtained in other calculations - SAGE

the function solve() in SAGE returns symbolic values for the variables i solve the equations for. for e.g:
sage: s=solve(eqn,y)
sage: s
[y == -1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) + 8*x^4 + 11*x^3 + 12*x^2)/(15*x + 1), y == 1/2*(sqrt(-596*x^8 - 168*x^7 - 67*x^6 + 240*x^5 + 144*x^4 - 60*x - 4) - 8*x^4 - 11*x^3 - 12*x^2)/(15*x + 1)]
My problem is that i need to use the values obtained for y in other calculations, but I cannot assign these values to any other variable. Could someone please help me with this?
(1) You should visit ask.sagemath.org, the Stack Overflow-like forum for Sage users, experts, and developers! </plug>
(2) If you want to use the values of a solve() call in something, then it's probably easiest to use the solution_dict flag:
sage: x,y = var("x, y")
sage: eqn = x**4+5*x*y+3*x-y==17
sage: solve(eqn,y)
[y == -(x^4 + 3*x - 17)/(5*x - 1)]
sage: solve(eqn,y,solution_dict=True)
[{y: -(x^4 + 3*x - 17)/(5*x - 1)}]
This option gives the solutions as a list of dictionaries instead of a list of equations. We can access the results like we would any other dictionary:
sage: sols = solve(eqn,y,solution_dict=True)
sage: sols[0][y]
-(x^4 + 3*x - 17)/(5*x - 1)
and then we can assign that to something else if we like:
sage: z = sols[0][y]
sage: z
-(x^4 + 3*x - 17)/(5*x - 1)
and substitute:
sage: eqn2 = y*(5*x-1)
sage: eqn2.subs(y=z)
-x^4 - 3*x + 17
et cetera. While IMHO the above is more convenient, you could also access the same results without solution_dict via .rhs():
sage: solve(eqn,y)[0].rhs()
-(x^4 + 3*x - 17)/(5*x - 1)
If you have unknown number of variables you can use **kwargs to pass data calculated with solve to next expressions. Here's example:
B_set is an list of variables and its filled in runtime so i dont know
names of variables and their quantity at the time of writing the code
solution = solve(system_of_equations, B_set)[0]
pretty_print(solution)
For example this gives me:
result of solving system of equations
I cant use this answer for further calculations so lets convert it to usable form
solution = {str(elem.lhs()): elem.rhs() for elem in solution}
Which gives us:
result converted to dictionary with string keys
Then we just pass this as **kwargs
approximation = approximation_function(**solution)
pretty_print(approximation)
And that converts this:
approximation without values from solution
Into this:
approximation with values from solution
Note: if you use dictionary output from solve() you still need to convert keys to string.

Resources