MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: ToExpression

  • To: mathgroup at smc.vnet.net
  • Subject: [mg35825] Re: [mg35697] ToExpression
  • From: Omega Consulting <omega_consulting at yahoo.com>
  • Date: Fri, 2 Aug 2002 02:42:28 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

At 03:16 AM 7/26/2002, Heubach, Silvia wrote:
>Hi all,
>
>I have a problem with using the function ToExpression to make a string
>into an expression that can then be evaluated. Something goes wrong with
>the evaluation - when I type the same expression in "by hand", then it
>evaluates properly. Either I am totally off in my understanding of how
>ToExpression works, or there is a potential bug (I am using version
>4.0). Please reply to this email
>(sheubac at calstatela.edu) rather than to the mathgroup, as I am
>travelling and cannot subscribe to the mathgroup right now - it would
>make my mailbox overflow....:)
>
>Thanks for any tips on this!
>Silvia
>
>********************************
>
>i = {1, 0, 0}; j = {0, 1, 0}; k = {0, 0, 1}; zero = {0, 0, 0};
>a = {i, j, k}
>
>str1 = "(((..)(..))(..))"
>
>
>
>The function insertx is to convert this string of parentheses and dots
>into a cross product, where each dot represents a different variable,
>and the parentheses indicate the groupings of the cross product.
>
>
>
>insertx[str_] := Module[{foo = str, vars, n, pos},
>     foo =
>       StringReplace[
>         foo, {")(" -> "),(", ".(" -> ".,(", ")." -> "),.", ".." ->
>".,."}];
>     foo = StringReplace[foo, {")" -> "]", "(" -> "Cross["}];
>          n = Length[StringPosition[foo, "."]];
>     Do[pos = StringPosition[foo, "."][[1]];
>           foo = StringReplacePart[foo, ToString[Slot[i]], pos]
>          (*Print[foo]*) , {i, n}];
>     ToExpression[foo]]
>
>
>
>The function properly converts the string into the associated cross
>product, but does not evaluate correctly. Checking whether what is
>returned by the function is still a string gives a false, and it should,
>as the function should return an expression. (the output is indented)
>
>insertx[str1]
>
>    ((#1\[Cross]#2)\[Cross](#3\[Cross]#4))\[Cross](#5\[Cross]#6)
>
>insertx[str1] &[j, k, i, k, k, i]
>
>    ((#1\[Cross]#2)\[Cross](#3\[Cross]#4))\[Cross](#5\[Cross]#6)
>
>StringQ[insertx[str1]]
>
>    False
>
>
>
>Typing in the corresponding cross product "by hand" and evaluating it
>returns the correct result.
>
>
>
>Cross[Cross[Cross[#1, #2], Cross[#3, #4]], Cross[#5, #6]] &[j, k, i, k,
>k, i]
>
>    {1, 0, 0}
>
>What is wrong??? I have spent many hours on trying to understand this,
>so either I am completely off or there is a bug.

This is an improper use of Slot. Instead, I would specify the vectors in 
the definition. Then you can use the vectors with Part:

In[1]:=
insertx[str_][lst_] := Module[{foo = str, n, pos},
     foo =
       StringReplace[
         foo, {")(" -> "),(", ".(" -> ".,(", ")." -> "),.", ".." ->
".,."}];
     foo = StringReplace[foo, {")" -> "]", "(" -> "Cross["}];
          n = Length[StringPosition[foo, "."]];
     Do[pos = StringPosition[foo, "."][[1]];
           foo = StringReplacePart[foo, ToString[lst[[i]]], pos]
          (*Print[foo]*) , {i, n}];
     ToExpression[foo]]

In[2]:=
insertx[str1][{j, k, i, k, k, i}]

Out[2]=
{1,0,0}

Or with a list operation like Fold:

In[3]:=
myinsertx[str_][ lst_] := Module[{foo = str},
     foo =
       StringReplace[
         foo, {")(" -> "),(", ".(" -> ".,(", ")." -> "),.", ".." ->
".,."}];
     foo = StringReplace[foo, {")" -> "]", "(" -> "Cross["}];
     foo = Fold[replace,foo,lst];
         ToExpression[foo]]

In[4]:=
replace[str_, item_]:=
   Module[{pos},
     pos=First[StringPosition[str,"."]];
     StringReplacePart[str, ToString[item], pos]
     ]

In[5]:=
insertx[str1][ {j, k, i, k, k, i}]

Out[5]=
{1,0,0}

--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"

Spend less time searching and more time finding.
http://www.wz.com/internet/Mathematica.html



  • Prev by Date: Re: Limit[Sin[a*x]/(a*x), x -> Infinity]
  • Next by Date: Table of Contents
  • Previous by thread: Re: RE: RE: Installing package "SpreadOption`"
  • Next by thread: Table of Contents