2

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.

micahscopes
  • 1,034
  • 1
  • 9
  • 12

2 Answers2

2

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.

kcrisman
  • 4,374
  • 20
  • 41
  • The "for all symbolic values of x" part would likely require you to do some nontrivial hacking to create your own substitution rule, though, and I'm not sure how to do that. – kcrisman Mar 23 '14 at 02:17
  • I'm working with some matrix equations and the operations I'm using don't associate with one another. The results are pretty lengthy (Sage even truncated them) and with lots of variables. Factoring helped a little, but didn't get rid of all the different even powers of sgn(x)^n. – micahscopes Apr 01 '14 at 18:46
1

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.

micahscopes
  • 1,034
  • 1
  • 9
  • 12