FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Forum index » Science and Technology » Math
mathematica: how to feed Solve output into Select?
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
Author Message
Peter Pein
science forum addict


Joined: 04 May 2005
Posts: 56

PostPosted: Wed Jul 19, 2006 9:39 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

markdemers15@hotmail.com schrieb:
Quote:
what documentation? i use their online mathematica documentation, and
they don't mention real or realQ. just mention ?positve and
?nonnegative.

Peter Pein wrote:
markdemers15@hotmail.com schrieb:
Thanks! It works.
Isn't it quite strange that mma uses
_?Positive and _?NonNegative
to query for positive numbers, but then mma uses
_Real
to query for reals. Why isn't it "_?Real" to maintain the same
pattern?


I'm sure you read the documentation carefully. So you should find the answer "_?RealQ" by yourself, shouldn't you?




Hello Mark,

my fault... Sometimes I forget having some packages in my init.m (in this case: DiscreteMath).

RealQ is in DiscreteMath`RSolve`.

Sorry,
Peter

P.S.: My remark regarding the documentation aimed mainly at the difference between "_Head" and "_?Function", which can easily be found in the docs (Ch. 2.3. of "The Mathematica Book").
Back to top
double d
science forum Guru


Joined: 03 May 2005
Posts: 640

PostPosted: Wed Jul 19, 2006 2:34 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

what documentation? i use their online mathematica documentation, and
they don't mention real or realQ. just mention ?positve and
?nonnegative.

Peter Pein wrote:
Quote:
markdemers15@hotmail.com schrieb:
Thanks! It works.
Isn't it quite strange that mma uses
_?Positive and _?NonNegative
to query for positive numbers, but then mma uses
_Real
to query for reals. Why isn't it "_?Real" to maintain the same
pattern?


I'm sure you read the documentation carefully. So you should find the answer "_?RealQ" by yourself, shouldn't you?

Back to top
Peter Pein
science forum addict


Joined: 04 May 2005
Posts: 56

PostPosted: Tue Jul 18, 2006 5:13 pm    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

markdemers15@hotmail.com schrieb:
Quote:
Thanks! It works.
Isn't it quite strange that mma uses
_?Positive and _?NonNegative
to query for positive numbers, but then mma uses
_Real
to query for reals. Why isn't it "_?Real" to maintain the same
pattern?


I'm sure you read the documentation carefully. So you should find the answer "_?RealQ" by yourself, shouldn't you?


Back to top
double d
science forum Guru


Joined: 03 May 2005
Posts: 640

PostPosted: Tue Jul 18, 2006 4:55 pm    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

Thanks! It works.
Isn't it quite strange that mma uses
_?Positive and _?NonNegative
to query for positive numbers, but then mma uses
_Real
to query for reals. Why isn't it "_?Real" to maintain the same
pattern?


Jean-Marc Gulliet wrote:
Quote:
magic math tricks wrote:
[...] how to
filter for only "Real" numbers (instead of Positive or Imaginary
numbers. In other words, I
want to do something like:

Cases[sol, {_ -> _?Real}]

except that it doesn't seem to understand "Real".

Say we solve the following equation w/ two real and two complex roots

sol = Solve[(x^2 + 2)*(x^2 - 2) == 0]

returns

{{x -> -Sqrt[2]}, {x -> (-I)*Sqrt[2]}, {x -> I*Sqrt[2]}, {x -> Sqrt[2]}}

Now we get rid of the complex values using

Cases[N[x /. sol], _Real]

that returns

{-1.4142135623730951, 1.4142135623730951}

HTH,
Jean-Marc
Back to top
Dana DeLouis
science forum beginner


Joined: 06 Mar 2006
Posts: 37

PostPosted: Tue Jul 18, 2006 1:17 pm    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

Here are some general ideas thrown out at random. Maybe there's something
here you can use:
You wanted the numbers in this format...

x /. Solve[x^4 + 4*x^3 - 7*x^2 - 22*x + 24 == 0]

{-4, -3, 1, 2}

Suppose it returned a mixed group of numbers...

data = Union[Flatten[Table[{j, N[j], j*I}, {j, -2, 2}]]]

{-2, -2., -1, -1., 0, -I, I, -2*I, 2*I, 0., 1, 1., 2, 2.}

Cases[data, _Real | _Integer]

{-2, -2., -1, -1., 0, 0., 1, 1., 2, 2.}

DeleteCases[data, _Complex]

{-2, -2., -1, -1., 0, 0., 1, 1., 2, 2.}

Cases[data, _?Positive]

{1, 1., 2, 2.}

Cases[data, _?NonNegative]

{0, 0., 1, 1., 2, 2.}

--
HTH. :>)
Dana DeLouis


"magic math tricks" <markdemers15@hotmail.com> wrote in message
news:1153204082.976405.301880@m73g2000cwd.googlegroups.com...
Quote:
wow! Thanks Jean and Ted. This works! I am in heaven if you can tell
me how to
filter for only "Real" numbers (instead of Positive or Imaginary
numbers. In other words, I
want to do something like:

Cases[sol, {_ -> _?Real}]

except that it doesn't seem to understand "Real".






Ted Hwa wrote:
In sci.math magic math tricks <markdemers15@hotmail.com> wrote:
: Thanks. Now you would make my life complete if you can tell me how to
: convert output such as { a1 -> 3.3 , b8 -> 4.8}
: into an array that looks like {3.3, 4.8}
: Any suggestiones?

{a1,b8} /. {a1 -> 3.3, b8 -> 4.8}

Ted
Back to top
Jean-Marc Gulliet
science forum beginner


Joined: 28 May 2005
Posts: 38

PostPosted: Tue Jul 18, 2006 8:06 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

magic math tricks wrote:
Quote:
[...] how to
filter for only "Real" numbers (instead of Positive or Imaginary
numbers. In other words, I
want to do something like:

Cases[sol, {_ -> _?Real}]

except that it doesn't seem to understand "Real".

Say we solve the following equation w/ two real and two complex roots

sol = Solve[(x^2 + 2)*(x^2 - 2) == 0]

returns

{{x -> -Sqrt[2]}, {x -> (-I)*Sqrt[2]}, {x -> I*Sqrt[2]}, {x -> Sqrt[2]}}

Now we get rid of the complex values using

Cases[N[x /. sol], _Real]

that returns

{-1.4142135623730951, 1.4142135623730951}

HTH,
Jean-Marc
Back to top
Peter Pein
science forum addict


Joined: 04 May 2005
Posts: 56

PostPosted: Tue Jul 18, 2006 7:37 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

magic math tricks schrieb:
Quote:
wow! Thanks Jean and Ted. This works! I am in heaven if you can tell
me how to
filter for only "Real" numbers (instead of Positive or Imaginary
numbers. In other words, I
want to do something like:

Cases[sol, {_ -> _?Real}]

except that it doesn't seem to understand "Real".






Ted Hwa wrote:
In sci.math magic math tricks <markdemers15@hotmail.com> wrote:
: Thanks. Now you would make my life complete if you can tell me how to
: convert output such as { a1 -> 3.3 , b8 -> 4.8}
: into an array that looks like {3.3, 4.8}
: Any suggestiones?

{a1,b8} /. {a1 -> 3.3, b8 -> 4.8}

Ted

as it seems to me you want a list of values (not rules) with some properties,

IMHO these are the easiest ways:

Select[x /. Solve[(x^2 + 1)(x^2 - 1) == 0, x], Element[#, Reals] &]

x /. {ToRules[Reduce[(x^2 + 1)(x^2 - 1) == 0, x, Reals]]}

Peter
Back to top
double d
science forum Guru


Joined: 03 May 2005
Posts: 640

PostPosted: Tue Jul 18, 2006 6:28 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

wow! Thanks Jean and Ted. This works! I am in heaven if you can tell
me how to
filter for only "Real" numbers (instead of Positive or Imaginary
numbers. In other words, I
want to do something like:

Cases[sol, {_ -> _?Real}]

except that it doesn't seem to understand "Real".






Ted Hwa wrote:
Quote:
In sci.math magic math tricks <markdemers15@hotmail.com> wrote:
: Thanks. Now you would make my life complete if you can tell me how to
: convert output such as { a1 -> 3.3 , b8 -> 4.8}
: into an array that looks like {3.3, 4.8}
: Any suggestiones?

{a1,b8} /. {a1 -> 3.3, b8 -> 4.8}

Ted
Back to top
Ted Hwa
science forum beginner


Joined: 03 May 2005
Posts: 43

PostPosted: Tue Jul 18, 2006 5:54 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

In sci.math magic math tricks <markdemers15@hotmail.com> wrote:
: Thanks. Now you would make my life complete if you can tell me how to
: convert output such as { a1 -> 3.3 , b8 -> 4.8}
: into an array that looks like {3.3, 4.8}
: Any suggestiones?

{a1,b8} /. {a1 -> 3.3, b8 -> 4.8}

Ted
Back to top
Jean-Marc Gulliet
science forum beginner


Joined: 28 May 2005
Posts: 38

PostPosted: Tue Jul 18, 2006 5:52 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

magic math tricks wrote:
Quote:
Thanks. Now you would make my life complete if you can tell me how to
convert output such as { a1 -> 3.3 , b8 -> 4.8}
into an array that looks like {3.3, 4.8}
Any suggestiones?

Dana wrote:
Would this idea work?

sol = Solve[x^4 + 4*x^3 -
7*x^2 - 22*x + 24 == 0]

{{x -> -4}, {x -> -3},
{x -> 1}, {x -> 2}}

Cases[sol, {_ -> _?Positive}]

{{x -> 1}, {x -> 2}}

--
HTH. :>)
Dana DeLouis

markdemers15@hotmail.com> wrote in message
news:1153175761.986651.321160@p79g2000cwp.googlegroups.com...
I have using Solve to solve coupled simultaneous equations for a vector
of values.
Solve comes up with a list of possible solutions.

Now, I wish to use Select to pick out the solutions that are in the
postive domain (e.g., every
element of the vector is non-negative and real). But Select seems to
require regular "lists"
as input. How do I convert my Solve output into Select input?

Jacky


Hi Jacky,


The Mathematica built-in function Solve returns a list of transformation
rules, each of one having the form "theVariable -> aValue". Use the
replacement operator "/." to get the values only as in the following
example (example that draws on Dana's answer):

sol = Solve[x^4 + 4*x^3 - 7*x^2 - 22*x + 24 == 0]

so sol is equal to the list of transformation rules

{{x -> -4}, {x -> -3}, {x -> 1}, {x -> 2}}

Now, using the replacement operator

x /. sol

yields the following list of values

{-4, -3, 1, 2}

as desired.

It might help to glance at section 1.4.2, "Values for Symbols", of _The
Mathematica Book Online_ at
http://documents.wolfram.com/mathematica/book/section-1.4.2

Regards,
Jean-Marc
Back to top
double d
science forum Guru


Joined: 03 May 2005
Posts: 640

PostPosted: Tue Jul 18, 2006 4:59 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

Thanks. Now you would make my life complete if you can tell me how to
convert output such as { a1 -> 3.3 , b8 -> 4.8}
into an array that looks like {3.3, 4.8}
Any suggestiones?

Dana wrote:
Quote:
Would this idea work?

sol = Solve[x^4 + 4*x^3 -
7*x^2 - 22*x + 24 == 0]

{{x -> -4}, {x -> -3},
{x -> 1}, {x -> 2}}

Cases[sol, {_ -> _?Positive}]

{{x -> 1}, {x -> 2}}

--
HTH. :>)
Dana DeLouis

markdemers15@hotmail.com> wrote in message
news:1153175761.986651.321160@p79g2000cwp.googlegroups.com...
I have using Solve to solve coupled simultaneous equations for a vector
of values.
Solve comes up with a list of possible solutions.

Now, I wish to use Select to pick out the solutions that are in the
postive domain (e.g., every
element of the vector is non-negative and real). But Select seems to
require regular "lists"
as input. How do I convert my Solve output into Select input?

Jacky
Back to top
Dana DeLouis
science forum beginner


Joined: 06 Mar 2006
Posts: 37

PostPosted: Tue Jul 18, 2006 2:21 am    Post subject: Re: mathematica: how to feed Solve output into Select? Reply with quote

Would this idea work?

sol = Solve[x^4 + 4*x^3 -
7*x^2 - 22*x + 24 == 0]

{{x -> -4}, {x -> -3},
{x -> 1}, {x -> 2}}

Cases[sol, {_ -> _?Positive}]

{{x -> 1}, {x -> 2}}

--
HTH. :>)
Dana DeLouis

<markdemers15@hotmail.com> wrote in message
news:1153175761.986651.321160@p79g2000cwp.googlegroups.com...
Quote:
I have using Solve to solve coupled simultaneous equations for a vector
of values.
Solve comes up with a list of possible solutions.

Now, I wish to use Select to pick out the solutions that are in the
postive domain (e.g., every
element of the vector is non-negative and real). But Select seems to
require regular "lists"
as input. How do I convert my Solve output into Select input?

Jacky
Back to top
double d
science forum Guru


Joined: 03 May 2005
Posts: 640

PostPosted: Mon Jul 17, 2006 10:36 pm    Post subject: mathematica: how to feed Solve output into Select? Reply with quote

I have using Solve to solve coupled simultaneous equations for a vector
of values.
Solve comes up with a list of possible solutions.

Now, I wish to use Select to pick out the solutions that are in the
postive domain (e.g., every
element of the vector is non-negative and real). But Select seems to
require regular "lists"
as input. How do I convert my Solve output into Select input?

Jacky
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [13 Posts] View previous topic :: View next topic
The time now is Thu Sep 20, 2018 7:29 am | All times are GMT
Forum index » Science and Technology » Math
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Eliminating multiple roots in mathematica's "Solve" function double d Symbolic 4 Sat Jul 15, 2006 11:49 am
No new posts How to solve linear program with matrix variable ? Fan num-analysis 4 Thu Jul 13, 2006 5:55 am
No new posts Thistle seed to solve Global Warming a_plutonium@hotmail.com Chem 9 Wed Jul 12, 2006 7:30 am
No new posts How to solve the linear objective function with max funct... Fan num-analysis 2 Tue Jul 11, 2006 7:05 pm
No new posts plz solve this math puzzle divya Recreational 10 Mon Jul 10, 2006 9:48 pm

Copyright © 2004-2005 DeniX Solutions SRL
Other DeniX Solutions sites: Electronics forum |  Medicine forum |  Unix/Linux blog |  Unix/Linux documentation |  Unix/Linux forums  |  send newsletters
 


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0186s ][ Queries: 20 (0.0026s) ][ GZIP on - Debug on ]