5
$\begingroup$

Dear StackExchange Community,

I have a lot of linearly independent vectors, e[i]e[j], e[i]e[j]h[i], f[i]e[j], etc, and I have an equation for example

a1 e[i]e[j] + 3a2 e[i]e[j] + a2 e[i]e[j]h[i] - a2 f[i]e[j] + a3 f[i]e[j] = 0.

I want to find a function which takes the equation (and nothing else), and returns the following system of linear equations:

a1 + 3a2 = 0, a2 = 0, - a2 + a3 = 0.

I know how to make that function (using patterns) if I specify vectors which appear in the equation. The problem is that my equation in reality is made of more than 100 vectors, so I am trying to avoid listing them.

Thank You very much!

$\endgroup$
0

1 Answer 1

8
$\begingroup$

This can be done using CoefficientRules:

expr = 
 a1 e[i] e[j] + 3 a2 e[i] e[j] + a2 e[i] e[j] h[i] - a2 f[i] e[j] + a3 f[i] e[j]
(* a1 e[i] e[j] + 3 a2 e[i] e[j] - a2 e[j] f[i] + a3 e[j] f[i] + a2 e[i] e[j] h[i] *)

components = DeleteDuplicates@Cases[expr, _e | _f | _h, All]
(* {e[i], e[j], f[i], h[i]} *)

coefficients = CoefficientRules[expr, components]
(* {{1, 1, 0, 1} -> a2, {1, 1, 0, 0} -> 
  a1 + 3 a2, {0, 1, 1, 0} -> -a2 + a3} *)

#2 == 0 & @@@ coefficients
(* {a2 == 0, a1 + 3 a2 == 0, -a2 + a3 == 0} *)
$\endgroup$
1
  • $\begingroup$ Thanks, this works perfectly! Can this be modified if I have noncommutative multiplication, for example e[i]**e[j], e[i]**e[j]**h[i], f[i]**e[j]? It seems to me that above doesn't work in this case. $\endgroup$
    – IvaMat
    Jan 10, 2023 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.