4
$\begingroup$

I have a system of equations and would like to retrieve the Jacobain w.r.t. to some variables. Now I see that this would be best done if I wrote the equations in a form == 0. Is there an easy way to do the latter? (or simply, retrieve the jacobian of this system of equations?).

Example below:

systemEq = {x1 == 2 x2 - 1,x1^2 == x2^2 - 4}

To make use of the D[] function, I imagine that we would have to rewrite this system as:

systemEqD = {x1 - ( 2 x2 - 1),x1^2 - ( x2^2 - 4)}

where implicitly I am aware that the equations ==0.

Is there an easy way to proceed? (ideally I would like to scale this for a larger number of equations). Many thanks.

$\endgroup$
1

3 Answers 3

5
$\begingroup$

What about this?

Cases[systemEq, Equal[lhs_, rhs_] :> lhs - rhs]

{1 + x1 - 2 x2, 4 + x1^2 - x2^2}

$\endgroup$
3
  • $\begingroup$ Perfect, just what I wanted. Many thanks. $\endgroup$
    – user191919
    Commented Jul 15, 2018 at 20:30
  • $\begingroup$ You're welcome. $\endgroup$ Commented Jul 15, 2018 at 20:30
  • 2
    $\begingroup$ Probably simpler: Equal -> Subtract? (untested) $\endgroup$ Commented Jul 15, 2018 at 22:01
3
$\begingroup$

You can also try SubtractSides designed for this sort of thing

ClearAll[x1,x2]
systemEq = {x1==2 x2-1,x1^2==x2^2-4};

First@SubtractSides[#] &/ @systemEq

Mathematica graphics

$\endgroup$
2
$\begingroup$

And,

systemEq = {x1 == 2 x2 - 1, x1^2 == x2^2 - 4};

#[[1]] - #[[-1]] == 0 & /@ systemEq

(* {1 + x1 - 2 x2 == 0, 4 + x1^2 - x2^2 == 0} *)
$\endgroup$

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.