How to express variable through other in SAGE - sage

Is there way to express mathematical expression through variable defined earlier in SAGE?
For example if I have variable a = b + c, I want SAGE rewrite expression b + c + d as a + d.
Thank you.

In fact, substituting such expressions is a nontrivial thing if you don't know what part of the expression tree you want. See Richard Fateman's comments here.
The core of the problem is that even the command that would do what you want is not about strings, but expressions.
sage: var("a b c d")
(a, b, c, d)
sage: (a+d).subs({a:b+c})
b + c + d
sage: (b+c+d).subs({b+c:a})
b + c + d
So you will have to use a "wildcard".
sage: w0 = SR.wild(0)
sage: (b+c+d).subs({b+c+w0:a+w0})
a + d
For more information, see
sage: x.match?
sage: SR.wild?
in the interactive shell or notebook.

As you can see in calculus, you can express d as variable with
a = var('a'); b+c
or like a function of b and c variable

Related

sage math: simplify algebra expression with steps

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.

How to create polynomials from strings

I would like to create Boolean polynomials from strings. At the moment I define polynomials by using implicitly defined variables:
R = BooleanPolynomialRing(names=["a", "b", "c"], order=TermOrder("lex"))
R.inject_variables()
f = 1 + a*b
g = a*b*(1+c)
I would like to define f and g in terms of strings "1 + a*b" and "a*b*(1+c)".
The polynomial ring can take strings and transform them into polynomials.
sage: R = BooleanPolynomialRing(names=["a", "b", "c"], order=TermOrder("lex"))
sage: R.inject_variables()
Defining a, b, c
sage: f = R("1 + a*b")
sage: f
a*b + 1
sage: g = R("a*b*(1+c)")
sage: g
a*b*c + a*b

In Julia, Transpose operator

I am trying to use a transpose operator over a vector in order to perform am element-wise addition.
For example, I want to add a column vector a = [a1;a2;a3] to a row vector b = [b1,b2] I should get matrix
M = a+b = [a1+b1, a1+b2; a2+b1, a2+b2; a3+b1, a3+b2].
In MATLAB it is equivalent (if both vectors are row vectors) M = a.'+b
I am trying to get the same in Julia but here is the problem, there is no .' operator in Julia starting from 1.0 version. There is the transpose operator which does not work in broadcasting mode. The adjoint operator is not Valid for me because I work with complex numbers.
a = Vector{ComplexF64}([1+3im,2])
b = Vector{ComplexF64}([0,0,0])
Z = zeros(ComplexF64,3,2)
G = zeros(ComplexF64,3,2)
#. Z = b + a' # Works but takes the complex conjugate
#. Z = b + transpose(a) # DOES NOT WORK!!!! The error is " DimensionMismatch("array could not be broadcast to match destination") "
Z = b .+ transpose(a) # Works but not efficient
#. Z = b + conj(a')
The third case Z = b .+ transpose(a) is not efficient because it makes 2 loops first one for addition b .+ transpose(a), than it runs the second loop one for the assignment of b .+ transpose(a) to Z. While the other 3 cases do it within one loop.
So which is the fastest way?
And why transpose doesn't within Broadcasting?
Thank you in advance
For Hermitian You can just type:
a' .+ b
Example
julia> a = ComplexF64.([1+3im,2])
2-element Array{Complex{Float64},1}:
1.0 + 3.0im
2.0 + 0.0im
julia> b = ComplexF64.([10,20,30])
3-element Array{Complex{Float64},1}:
10.0 + 0.0im
20.0 + 0.0im
30.0 + 0.0im
julia> a' .+ b
3×2 Array{Complex{Float64},2}:
11.0-3.0im 12.0+0.0im
21.0-3.0im 22.0+0.0im
31.0-3.0im 32.0+0.0im
If you want to have just transposition you could define your own unary operator (perhaps from the list of unused unary operators):
¬(a) = permutedims(a)
Now you could do
julia> ¬a
1×2 Matrix{ComplexF64}:
1.0+3.0im 2.0+0.0im

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

Resources