Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #14054 > unrolled thread

extract from Reduce

Started byhamiltoncycle@gmail.com
First post2013-04-04 17:10 +0000
Last post2013-04-05 02:30 +0000
Articles 4 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  extract from Reduce hamiltoncycle@gmail.com - 2013-04-04 17:10 +0000
    Re: extract from Reduce awnl <awnl@gmx-topmail.de> - 2013-04-05 02:29 +0000
    Re: extract from Reduce debguy <johnandsara2@cox.net> - 2013-04-05 02:29 +0000
    Re: extract from Reduce Peter Pein <petsie@dordos.net> - 2013-04-05 02:30 +0000

#14054 — extract from Reduce

Fromhamiltoncycle@gmail.com
Date2013-04-04 17:10 +0000
Subjectextract from Reduce
Message-ID<kjkc5k$7n0$1@smc.vnet.net>
I have a result from the command Reduce that looks like this (as an example):

a:= x==3||x==4||x==7

I need to extract the numbers but I can't find a nice way to handle the case when there is only one equation, for instance:

a:= x==6

currently I check what the head of the expression is (Head[a]) and handle the case Head[a]=Or with a Map[] and the case Head[a]="Equal" with a[[2]]. Not very nice, but I guess that there is a neat way to do it.

cheers, Robert

[toc] | [next] | [standalone]


#14058

Fromawnl <awnl@gmx-topmail.de>
Date2013-04-05 02:29 +0000
Message-ID<kjlcuj$b5s$1@smc.vnet.net>
In reply to#14054
Hi,

> I have a result from the command Reduce that looks like this (as an
> example):
>
> a:= x==3||x==4||x==7
>
> I need to extract the numbers but I can't find a nice way to handle
> the case when there is only one equation, for instance:
>
> a:= x==6
>
> currently I check what the head of the expression is (Head[a]) and
> handle the case Head[a]=Or with a Map[] and the case Head[a]="Equal"
> with a[[2]]. Not very nice, but I guess that there is a neat way to
> do it.
>

There might be more efficient ways to do this, but I think this is clear 
and compact:

Cases[{a}, Equal[x, v_?NumericQ] :> v, Infinity]

hth,

albert

[toc] | [prev] | [next] | [standalone]


#14060

Fromdebguy <johnandsara2@cox.net>
Date2013-04-05 02:29 +0000
Message-ID<kjlcv3$b69$1@smc.vnet.net>
In reply to#14054
this will get a num list if ==, !=, ||, && are involved

Begin["a`"]
Reduce[p*x == 1 && x^2 == y, {p, x}];
% /. x_ == y_ -> {x, y} /. x_ != y_ -> {x, y}; (* 1 *)
% //. x_ && y_ -> {x, y} //. x_ || y_ -> {x, y};
Partition[Flatten[%], 2]; (* 2 *)
answer=%[[All,2]] (* just the numbers so to speak *)
End[];
Scan[Remove, Names["a`*"]]

(* note!  without !=, &&, || values are "wrong" unless used in context
*)

(* (1)
%//InputForm
{p, -(1/Sqrt[y])} && {x, -Sqrt[y]} && {y, 0} ||
 {p, 1/Sqrt[y]} && {x, Sqrt[y]} && {y, 0}
*)

(* 2
% // InputForm
{{p, -(1/Sqrt[y])}, {x, -Sqrt[y]}, {y, 0}, {p, 1/Sqrt[y]}, {x,
Sqrt[y]},
 {y, 0}}
*)

[toc] | [prev] | [next] | [standalone]


#14062

FromPeter Pein <petsie@dordos.net>
Date2013-04-05 02:30 +0000
Message-ID<kjlcve$b6g$1@smc.vnet.net>
In reply to#14054
Am 04.04.2013 19:10, schrieb hamiltoncycle@gmail.com:
> I have a result from the command Reduce that looks like this (as an example):
>
> a:= x==3||x==4||x==7
>
> I need to extract the numbers but I can't find a nice way to handle the case when there is only one equation, for instance:
>
> a:= x==6
>
> currently I check what the head of the expression is (Head[a]) and handle the case Head[a]=Or with a Map[] and the case Head[a]="Equal" with a[[2]]. Not very nice, but I guess that there is a neat way to do it.
>
> cheers, Robert
>

Hi Robert,

  say you've got a quite simple result of Reduce:

In[1]:= InputForm[a = Reduce[Mod[x, 3] == Mod[1 + x^2, 5], x, Integers]]
Out[1]//InputForm=
Element[C[1], Integers] && (x == 3 + 15*C[1] || x == 10 + 15*C[1] ||
   x == 11 + 15*C[1] || x == 12 + 15*C[1] || x == 14 + 15*C[1])

then

In[2]:= Cases[LogicalExpand[a] /. And | Or -> List,
     HoldPattern[Equal[v___, x, w___]] :> Sequence[v, w],
     Infinity] // InputForm
Out[2]//InputForm=
{3 + 15*C[1], 10 + 15*C[1], 11 + 15*C[1], 12 + 15*C[1], 14 + 15*C[1]}


in your even simpler case
a= x==3||x==4||x==7

Refine[x, #]& /@ List @@ a

would be sufficient to get
{3, 4, 7}

hth,
Peter

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web