Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

With a text file with entries formatted as (output from C++):

f[38.67] = -2.5387862698183892298317350539374412777263289550697e-05;
⋮

Is there a way to read this into Mathematica? It seems to be getting confused with the e, thinking it is just a symbol e.

share|improve this question
2  
    
When reading Python output in the past I have once or twice done a global replacement of e with *^. That seems like the easiest approach if you have this file already in place. –  Oleksandr R. Nov 22 '12 at 14:59
    
Could be, is there an automated way to get Mathematica to do this? I am going to have lots of files in this format..(I was thinking about changing the output of c++ code to non scientific but it's not too easy with the MPFR data type and c++ wrapper I am using) –  fpghost Nov 22 '12 at 15:19
    
Alternatively, you could Get the file via a pipe, preprocessing it with sed or similar. –  Oleksandr R. Nov 22 '12 at 15:30
3  
Perhaps something like <<"!sed 's/e/*^/gI' < file.m"? Obviously you should be careful when doing this if you have other occurrences of "e" in the file. –  Oleksandr R. Nov 22 '12 at 15:47
show 1 more comment

2 Answers 2

up vote 3 down vote accepted

Similar to Rolf's method:

string = "f[38.67]=-2.5387862698183892298317350539374412777263289550697e-05;";

StringReplace[string, {"e+" :> "*^", "e-" :> "*^-"}];

ToExpression@%;

?? f
Global`f

f[38.67] = -0.000025387862698183892298317350539374412777263289550697

Depending on your data you may want a more specific pattern, e.g.:

StringReplace[string,
  {a : NumberString ~~ "e" ~~ b : NumberString :> a <> "*^" <> b}]
share|improve this answer
    
+1 That's neater. –  Rolf Mertig Nov 23 '12 at 1:15
2  
Regex fun: StringReplace[string, RegularExpression["e([+-]?)(\\d+)"] :> "*^$1$2"] –  J. M. Nov 23 '12 at 2:43
    
@J.M. you are a glutton for punishment, aren't you! –  Oleksandr R. Nov 24 '12 at 20:39
    
@Oleksandr... but I like regexes! :D Also, I like the generality: it can handle all of "2.9e5", "1.7e+2", and "3.16e-3". –  J. M. Nov 25 '12 at 1:22
add comment

Import usually automatically converts the e format to powers of. You can use ImportString with the "Table" or "List" type:

ImportString["-2.5387862698183892298317350539374412777263289550697e-05", "Table"]

{{-0.000025387862698183892298317350539374412777263289550697}}

or

ImportString["1.002e-26", "Table"]

{{1.002*10^-26}}

share|improve this answer
    
thanks, but what if I want the input to initialize this function f rather than just reading the RHS values and putting them in a list? –  fpghost Nov 22 '12 at 13:29
    
I had previously just been using Get to do this. –  fpghost Nov 22 '12 at 14:21
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.