RE: ToExpression
- To: mathgroup at smc.vnet.net
- Subject: [mg35714] RE: [mg35697] ToExpression
- From: "DrBob" <majort at cox-internet.com>
- Date: Sat, 27 Jul 2002 06:43:15 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
Just a small change:
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 <> "&"]] (* put & here *)
insertx[str1]
insertx[str1][j, k, i, k, k, i] (* not here *)
Bobby Treat
-----Original Message-----
From: Heubach, Silvia [mailto:sheubac at exchange.calstatela.edu]
To: mathgroup at smc.vnet.net
Subject: [mg35714] [mg35697] ToExpression
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.