# How to force the substitution since subs is not working?

I have an expression containing alpha_1, beta_1, gamma_1.

They are normalized by the constraint alpha_1^2 + beta_1^2 + gamma_1^2 = 1.

That would simplify the expression. Somehow subs doesn't work.

After defining dkkr1 as

dkkr1 = 2*I*pi*sqrt(-c^2*gamma_1^2 - (alpha_1^2 + beta_1^2)*c^2 + v2^2)*sqrt((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*beta_1/(c^2*gamma_1^2 + (alpha_1^2 + beta_1^2)*c^2 - v2^2) - 2*(-I*pi*sqrt(-c^2*gamma_1^2 - (alpha_1^2 + beta_1^2)*c^2 + v2^2)*sqrt((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*alpha_1^2*c^4 - I*pi*sqrt(-c^2*gamma_1^2 - (alpha_1^2 + beta_1^2)*c^2 + v2^2)*sqrt((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*c^4*gamma_1^2 + (I*pi*sqrt(-c^2*gamma_1^2 - (alpha_1^2 + beta_1^2)*c^2 + v2^2)*sqrt((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*beta_1^2*v1^2 + I*pi*sqrt(-c^2*gamma_1^2 - (alpha_1^2 + beta_1^2)*c^2 + v2^2)*sqrt((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*c^2)*v2^2)*Delta/(c^6*gamma_1^4 + 2*(alpha_1^2 + beta_1^2)*c^6*gamma_1^2 + (alpha_1^4 + 2*alpha_1^2*beta_1^2 + beta_1^4)*c^6 + ((alpha_1^2 + beta_1^2 + gamma_1^2)*v1^2 + c^2)*v2^4 + (c^4*gamma_1^6 + 3*(alpha_1^2 + beta_1^2)*c^4*gamma_1^4 + 3*(alpha_1^4 + 2*alpha_1^2*beta_1^2 + beta_1^4)*c^4*gamma_1^2 + (alpha_1^6 + 3*alpha_1^4*beta_1^2 + 3*alpha_1^2*beta_1^4 + beta_1^6)*c^4)*v1^2 - 2*(c^4*gamma_1^2 + (alpha_1^2 + beta_1^2)*c^4 + (c^2*gamma_1^4 + 2*(alpha_1^2 + beta_1^2)*c^2*gamma_1^2 + (alpha_1^4 + 2*alpha_1^2*beta_1^2 + beta_1^4)*c^2)*v1^2)*v2^2)


I try to substitute

dkkr1= dkkr1.subs(sqrt(1- alpha_1^2 - beta_1^2)  ==  gamma_1)
show(dkkr1)


I am new to SageMath and would love to learn how to make this work.

edit retag close merge delete

Sort by » oldest newest most voted

The thing to understand is that the subs method work at the level of the expression tree : when the pattern is found in the tree, it is replaced.

For example, 2*(a + b) is represented as the following tree:

*
├── 2
└── +
├── a
└── b


However, the addition is not considered as a binary operation, but as an operation of arbitrary arity, hence 2*(a + b + c) is not represented as

*
├── 2
└── +
├── a
└── +
├── b
└── c


but as

*
├── 2
└── +
├── a
├── b
└── c


This implies that a+b is not a subtree of a+b+c.

In particular, you have to define two different rules to replace alpha_1^2 + beta_1^2 with 1 - gamma_1^2 and alpha_1^2 + beta_1^2 + gamma_1^2 with 1.

In terms of code, the previous explanations translate to:

sage: dkkr1.subs(alpha_1^2 + beta_1^2 == 1-gamma_1^2, alpha_1^2 + beta_1^2 + gamma_1^2 == 1)

more