sage math: simplify algebra expression with steps - sage

Example:
simplify 2(3x+5)
step1: 2*3x + 2*5
step2: 6x + 2*5
solution: 6x + 10
I tried sage math: https://sagecell.sagemath.org/?q=ngzkdm
sage: x = SR.var('x')
sage: 2*(3*x+5)
6*z + 10
can any quide how this can be done
i need to show the steps. of course this a a simple case.

Searching the web for [ math software steps ] or [ math software show steps ] reveals results such as cymath, mathway, polymathlove, quickmath, symbolab, wolfram alpha, etc.
They seem to be mostly apps and websites; I could not locate
any free software for that task, though it may exist.
That is not SageMath's focus, but one could still give it a go.
Sage has "held expressions":
sage: x = SR.var('x')
sage: a = 3*x + 5
sage: b = SR(2)
sage: expr = a.mul(b, hold=True)
sage: expr
2*(3*x + 5)
One can get an expression's operator and operands.
sage: a.operator()
<function add_vararg at 0x...>
sage: a.operands()
[3*x, 5]
sage: expr.operator()
<function mul_vararg at 0x...>
sage: expr.operands()
[3*x + 5, 2]
The sum and product symbolic operators are:
sage: plus = sage.symbolic.operators.add_vararg
sage: times = sage.symbolic.operators.mul_vararg
sage: expr.operator() == times
True
sage: a.operator() == plus
True
So one could walk the expression tree and
expand any products of sums in it.
Here is a step by step expansion of the simple example
in the question, done by hand:
sage: x = SR.var('x')
sage: a = 3*x + 5
sage: b = SR(2)
sage: expr = a.mul(b, hold=True)
sage: expr
2*(3*x + 5)
sage: d, e = a.operands()
sage: d, e
(3*x, 5)
sage: print(f'{b}*{d} + {b}*{e}')
2*3*x + 2*5
sage: print(f'{b*d} + {b*e}')
6*x + 10
sage: print(b*a)
6*x + 10
One could write a function to
analyse the expression tree
detect all products one of whose factors is a sum
ask the user which expansion to perform
print the steps of the chosen expansion
repeat
It would involve a lot of trial and error to get it right.
Hopefully the hints above help a little.

Related

Sage for Galois Field

I have a problem in Galois Field in SageMath. I can't convert a binary to polynomial.
If I have a binary number, 1010101 how do I convert this number in polynomial 1010101 = x^6+x^4+x^2+1.
I do not know if there is an inbuilt way (I presume you have already looked also), but you can always do the following:
sage: P.<x> = PolynomialRing(ZZ)
sage: binString = "1010101"
sage: arrayOfTerms = [0]*len(binString)
sage: binString = binString[::-1] #Flip it so that the first digit corresponds to the constant term
sage: for i in xrange(len(binString)):
....: arrayOfTerms[i] = (x**i)*int(binString[i])
....:
sage: poly = sum(arrayOfTerms)
sage: poly
x^6 + x^4 + x^2 + 1

Can I substitute using a symbolic equation in Sage?

I am using the following map in Sage:
f = lambda x: sgn(x)*sgn(x);
which evaluates to f(x) = 0 for x=0 and f(x)=1 for x!=0;
In symbolic results, sgn(x)^2, sgn(x)^4 and sgn(x)^8, etc. are being treated as unequal, even though they are equal for all values of x. Is there a way that I can substitute something like:
sgn(x)^2 == sgn(x)^4 == sgn(x)^8
for all occurrences of these relations, and for all symbolic values of x?
I could create a new substitution rule for every symbol, e.g.
result.subs(sgn(c)^2 == sgn(c)^4).subs(sgn(d)^2 == sgn(d)^4)...
and so on, but that seems hard to control.
This is perhaps a dumb question for me to ask... is the nature of your result one that you could just factor?
sage: f(x) = sgn(x)^2
sage: f
x |--> sgn(x)^2
sage: Z = (1+f)^3
sage: Z = Z.expand()
sage: Z
x |--> sgn(x)^6 + 3*sgn(x)^4 + 3*sgn(x)^2 + 1
sage: Z.factor()
x |--> (sgn(x)^2 + 1)^3
In which case it makes your question moot, hopefully:
sage: Z.subs(sgn(x)^2==x)
x |--> (x + 1)^3
not that that is your subs, just as an example.
Apparently Sage allows for the use of wildcards in substitution (here's the example that tipped me off). So I did something like:
var('a,b,c,d,e,f');
w = SR.wild(0);
result = f(a,b,c,d,e,f).subs(sgn(w)^4 == sgn(w)^2).subs(sgn(w)^8 == sgn(w)^2);
And it worked! Much easier.

Solve Binary Linear System + SAGE

I have the equation Ax=b where all matrix entries and all entrie of vector b are in GF(2). How I will be able to solve this linear system equation over GF(2) in SAGE software?
This was answered on the sage-support mailing list. For completeness, I'll basically cut and paste the answer from there:
sage: A = random_matrix(GF(2), 10000, 10000)
sage: A.det()
1
sage: b = random_vector(GF(2), 10000)
sage: x = A \ b
sage: A * x == b
True

Sage: Finding linear combination of a specified basis giving a specified element?

Given a vector space V, a basis B (list of tuples each of size corresponding to the dimension of V and over the same field) and a vector v - what is the sage command to find the (unique) linear combination of the elements of B giving v?
IIUC, you probably want to do something like this:
sage: basis = [(2,3,4),(1,23/4,3), (9,8/17,11)]
sage: F = QQ
sage: F
Rational Field
sage:
sage: # build the vector space
sage: dim = len(basis[0])
sage: VS = (F**dim).span_of_basis(basis)
sage: VS
Vector space of degree 3 and dimension 3 over Rational Field
User basis matrix:
[ 2 3 4]
[ 1 23/4 3]
[ 9 8/17 11]
and then, having constructed the vector space over the field:
sage: # choose some random vector
sage: v = vector(F, (19/2, 5/13, -4))
sage: v
(19/2, 5/13, -4)
sage:
sage: # get its coordinates
sage: c = VS.coordinate_vector(v)
sage: c
(-470721/19708, 59705/4927, 24718/4927)
sage: parent(c)
Vector space of dimension 3 over Rational Field
sage:
sage: # aside: there's also .coordinates(), but that returns a list instead
sage:
sage: # sanity check:
sage: VS.basis_matrix().transpose() * c
(19/2, 5/13, -4)
sage: VS.basis_matrix().transpose() * c == v
True

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