Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top