Message Boards Message Boards

0
|
3911 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How do I apply boolean test to each entry / matrix element?

Posted 10 years ago

In an original matrix/table, each entry consist of two solutions.

I make a new matrix/table by taking Max - Min of each element/entry.

In more complex equations, within some range each matrix element has two or more solutions, and in some range it's single-solution.

The way to distinguish them is to see if (Max-Min) > 0 or = 0.

Then if (Max-Min) > 0, it implies MultiValuedTest is true. Otherwise, false.

For regions where MultiValuedTest is true, $z = -1$ otherwise, $ z = y^2 $.

What I'm missing here is how to apply boolean test to each element.

tx = Table[x /. Solve[x^2 - 3 == y, x], {y, 0, 4, 1}]
z[y_] := y^2
state = (Max[#] - Min[#]) & /@ tx
MultiValuedTest[x_] := Module[{diff}, diff = 0; Return[state > diff]]
Table[If[MultiValuedTest[x], -1, z], {y, 0, 4, 1}]

For this simple equation, MultiValuedTest should read {true,true,true,true,true} and table of $z$ should be {-1,-1,-1,-1,-1}.

POSTED BY: Jack Dough
2 Replies
Posted 10 years ago

I wrote a code - it only works for a simple equation. For a more complex one below, it doesn't!

I'm trying to observe how the solution for p3 changes with small 'additions' to the equation.

Code working on simple equation

tabletry = (  Table[p1*p2, {p1, 0, 3}, {p2, 0, 5}]  ) // MatrixForm
tabletest = (  Map[ {# == 0} &, tabletry, {-1} ]  )  // MatrixForm
deltap = 10^-2;

enter image description here

enter image description here

eqn1[p1_?NumericQ, p2_?NumericQ, p3_] := 1/p3 - 5 p3 ==  p1 + p2

eqn2[p1_?NumericQ, p2_?NumericQ, p3_] := 1/p3 - 5 p3 + deltap == p1 + p2

diff[p1_?NumericQ, 
  p2_?NumericQ] := (p3 /. FindRoot[eqn2[p1, p2, p3], {p3, 1}]) - (p3 /. FindRoot[eqn1[p1, p2, p3], {p3, 1}])

(Array[# #2 /. {(0) -> Quiet@Style[Evaluate[diff[#, #2]], Red], _ :> 0} &, {4, 6}, 0]) // MatrixForm

enter image description here

Code NOT working on more complex equation

Variables

C1 = 10^(-1);
C2 = 0.1*C1;
R = 50;
Tb = 0.1;
Geb = 5.;
Z0 = 50;
L[Te_] := 1. + 1.*(Te - 0.1);
Zlcr[Te_, w_] := (1/R + 1/(I*L[Te]*w) + I*C1*w)^-1;
Zload[Te_, w_] := -I*w*C2 + Zlcr[Te, w];
\[CapitalGamma][Te_, w_] := (Zload[Te, w] - Z0)/(Zload[Te, w] + Z0);
y[Te_, w_] := (Abs[\[CapitalGamma][Te, w]])^2;
p[Te_, w_] := Abs[\[CapitalGamma][Te, w]]
DeltaPlocal = 10.^-5;

Table of values

tt2 = Table[ Te /. Chop@Solve[  (1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb, Te , Method -> Reduce] ,  
{w, 2.5, 3., 0.1}  , {Pprobe, 1., 2.5, 0.1} ];

tttt2 = ( Map[Chop@(Max[#] - Min[#] &), tt2, {2}] ) // MatrixForm
testmatrix = Map[ {# == 0} &, tttt2, {-1} ]

enter image description here enter image description here

eqn8[w_?NumericQ, Pprobe_?NumericQ, Te_] := (1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb
eqn9[w_?NumericQ, Pprobe_?NumericQ, Te_] := DeltaPlocal + (1 -   y[Te, w]) Pprobe  ==  (Te - Tb) Geb 
diff2[w_?NumericQ, Pprobe_?NumericQ] :=  ((Te /. FindRoot[eqn9[w, Pprobe, Te], {Te, 0.2}]) - (Te /. FindRoot[eqn8[w, Pprobe, Te], {Te, 0.2}]))
(Array[# #2 /. {(0) -> Quiet@Style[Evaluate[diff2[#, #2]], Red], _ :>  0} &, {6, 16}, 0]) // MatrixForm

enter image description here

POSTED BY: Jack Dough

I follow your code up to MultiValuedTest. In MultiValuedTest what is the purpose of diff (which is simply 0 there, so MultiValuedTest could just be defined to be state>0) and where is a dependency of MultiValuedTest on x on the right hand side? Also--just a quick comment on coding--you should define MultiValuedTest in terms of a delayed evaluation (:=) instead of immediate evaluation(=). And Return is never necessary: Mathematica returns the final result of a sequence of expressions.

However, if your question is how to apply a function to the elements of a matrix you can use Map.

For example, here is a random 5x5 matrix:

mat = RandomReal[{0, 1}, {5, 5}]

And you can use Map to apply a function f to all its elements individually in this way:

Map[f, mat, {2}]

If this is not your question could you clean up your code so that it works and then rephrase?

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract