11
$\begingroup$

I want to divide a matrix: mat = {{a, 0, -b}, {d, a, 0}, {0, 0, -a}}

with the following assumptions: a >= 0, b >= 0, d >= 0

Into two matrices, one containing the positive elements, and one the negative ones, given a set of assumptions.

I want to get:

mat2 = {{a,0,0},{d,a,0},{0,0,0}}

And:

mat3 = {{0,0,b},{0,0,0},{0,0,a}}

$\endgroup$
4
  • 3
    $\begingroup$ One way is mat /. Times[-1, a_] :> 0 for the first, and %-mat for the second $\endgroup$ – Jason B. Apr 6 '16 at 12:58
  • 3
    $\begingroup$ mat /. -1 :> 0 $\endgroup$ – garej Apr 6 '16 at 17:16
  • $\begingroup$ "extract" how? additively, multiplicatively, quadratically, either or both? $\endgroup$ – smci Apr 7 '16 at 17:35
  • $\begingroup$ My goal is to get any term with a minus sign into one matrix, and any term with a positive sign into another matrix. Even if they are part of the same matrix cell. For example: From the matrix mat mat = {{a-c, 0, d-b}, {d, a-bc, 0}, {0, 0, -a}} Get additively the matrices mat2 = {{a,0,d},{d,a,0},{0,0,0}} mat3 = {{c,0,b},{0,bc,0},{0,0,a}} Such that: mat=mat2-mat3 $\endgroup$ – Luis Fernando Apr 21 '16 at 14:56
14
$\begingroup$
Simplify[MapThread[Max, {mat, 0 mat}, 2],  Assumptions -> {a > 0, b > 0, d > 0}]
(* {{a, 0, 0}, {d, a, 0}, {0, 0, 0}} *)

Simplify[MapThread[Max, {-mat, 0 mat}, 2], Assumptions -> {a > 0, b > 0, d > 0}]
(* {{0, 0, b}, {0, 0, 0}, {0, 0, a}} *)
$\endgroup$
14
$\begingroup$

Using an undocumented function:

mat /. x_?Internal`SyntacticNegativeQ :> 0
   {{a, 0, 0}, {d, a, 0}, {0, 0, 0}}

% - mat
   {{0, 0, b}, {0, 0, 0}, {0, 0, a}}
$\endgroup$
7
  • $\begingroup$ The output for the substraction of matrices is not simplified, even after applying FullSimplify. Is there another way? Thank you for everything! $\endgroup$ – Luis Fernando Apr 6 '16 at 15:19
  • $\begingroup$ Can you edit your question showing the case you mention, and the result you expect? $\endgroup$ – J. M.'s ennui Apr 6 '16 at 15:22
  • $\begingroup$ The output for the subtraction of matrices is not simplified even after applying FullSimplify. I expected: % - mat {{0, 0, b}, {0, 0, 0}, {0, 0, a}} I got: % - mat {{a, 0, 0}, {d, a, 0}, {0, 0, 0}} - {{a, 0, -b}, {d, a, 0}, {0, 0, -a}} Is there another way? Thank you for everything! $\endgroup$ – Luis Fernando Apr 6 '16 at 15:30
  • $\begingroup$ Did you run mat /. x_?Internal`SyntacticNegativeQ :> 0 and % - mat in separate cells? $\endgroup$ – J. M.'s ennui Apr 6 '16 at 15:34
  • 1
    $\begingroup$ @LuisFernando you could also use (-mat) /. x_?Internal`SyntacticNegativeQ :> 0 for mat3 $\endgroup$ – Algohi Apr 6 '16 at 18:28
5
$\begingroup$
Map[0 &, mat, {3}]

{{a, 0, 0}, {d, a, 0}, {0, 0, 0}}

Map[0 &, #, {3}] - # &@mat

{{0, 0, b}, {0, 0, 0}, {0, 0, a}}

$\endgroup$
2
$\begingroup$
mat = {{a, 0, -b}, {d, a, 0}, {0, 0, -a}};
(mat2 = PowerExpand@ComplexExpand@((# + Abs[#])/2) &@mat) // MatrixForm
(mat3 = -PowerExpand@ComplexExpand@((# - Abs[#])/2) &@mat) // MatrixForm

enter image description here

Alternatively, but perhaps not as robust,

(mat2 = (# + (# /. -x_ :> x))/2 &@mat) // MatrixForm
(mat3 = mat2 - mat) // MatrixForm
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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