ToExpression
- To: mathgroup at smc.vnet.net
- Subject: [mg35697] ToExpression
- From: "Heubach, Silvia" <sheubac at exchange.calstatela.edu>
- Date: Fri, 26 Jul 2002 04:16:45 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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.