MathGroup Archive 2007

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

Search the Archive

Re: BinaryWrite and Position in Mathematica 6

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83319] Re: BinaryWrite and Position in Mathematica 6
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 17 Nov 2007 05:27:13 -0500 (EST)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fhjr8v$4ce$1@smc.vnet.net>

L. Dwynn Lafleur wrote:
> I do not understand the following results from Mathematica 6.0.1.0 running 
> in Windows XP (SP2):
> 
> In[1]:= a={{2}}
> Out[1]= {{2}}
> 
> In[2]:= b=Position[{x,y,z},y]
> Out[2]= {{2}}
> 
> In[3]:= a==b
> Out[3]= True
> 
> 
>>From the above results, we see that although they produced differently, a 
> and b are apparently identical.  Now I attempt to write each to a file using 
> BinaryWrite:
> 
> In[4]:= BinaryWrite["aFile.dat",a,"Integer32"]
> Out[4]= aFile.dat
> 
> In[5]:= BinaryWrite["bFile.txt",b,"Integer32"]
>     During evaluation of In[5]:= BinaryWrite::nocoerce: {2} cannot be 
> coerced to the specified format. >>
> Out[5]= $Failed
> 
> BinaryWrite writes a successfully, but fails on b.  However, consider the 
> following:
> 
> In[6]:= BinaryWrite["bFile2.txt",ToExpression[b],"Integer32"]
> Out[6]= bFile2.txt
> 
> Why do I need to convert b before writing it to a file?  I thought that 
> "a==b -> True" indicates a and b are identical and therefore should be 
> treated in the same way by Mathematica.  What am I overlooking?

Very puzzling, indeed. First, I thought that you might have a problem 
with your environment. However, I did reproduce the same behaviors 
without any difficulty on my system. Then, ...

Even SameQ (triple equal sign ===), which is a stronger test than Equal 
(double equal sing ==), cannot find any difference. Comparing the full 
form of each expression just reinforces the feeling that there exists no 
difference. And I fail to see what ToExpression can possibly change (and 
so does Mathematica, except BinaryWrite of course!). I hope someone else 
will be able to shed some light.

In[1]:= a = {{2}}

Out[1]= {{2}}

In[2]:= b = Position[{x, y, z}, y]

Out[2]= {{2}}

In[3]:= a == b

Out[3]= True

In[4]:= a === b

Out[4]= True

In[5]:= FullForm[a]

Out[5]//FullForm= \!\(\*
TagBox[
StyleBox[
RowBox[{"List", "[",
RowBox[{"List", "[", "2", "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

In[6]:= FullForm[b]

Out[6]//FullForm= \!\(\*
TagBox[
StyleBox[
RowBox[{"List", "[",
RowBox[{"List", "[", "2", "]"}], "]"}],
ShowSpecialCharacters->False,
ShowStringCharacters->True,
NumberMarks->True],
FullForm]\)

In[7]:= OwnValues[a]

Out[7]= {HoldPattern[a] :> {{2}}}

In[8]:= OwnValues[b]

Out[8]= {HoldPattern[b] :> {{2}}}

In[9]:= ToExpression[b]

Out[9]= {{2}}

In[10]:= b === ToExpression[b]

Out[10]= True

In[11]:= BinaryWrite["aFile.dat", a, "Integer32"]

Out[11]= "aFile.dat"

In[12]:= BinaryWrite["bFile.dat", b, "Integer32"]

During evaluation of In[12]:= BinaryWrite::nocoerce: {2} cannot be \
coerced to the specified format. >>

Out[12]= $Failed

In[13]:= BinaryWrite["bFile2.txt", ToExpression[b], "Integer32"]

Out[13]= "bFile2.txt"

In[14]:= $Version

Out[14]= "6.0 for Microsoft Windows (32-bit) (June 19, 2007)"

Regards,
-- 
Jean-Marc


  • Prev by Date: Re: Minimizing Mathematica file size for source code control?
  • Next by Date: Re: memory release problem in mathematica6.0
  • Previous by thread: BinaryWrite and Position in Mathematica 6
  • Next by thread: Version 4: Zoom2D and GetGraphicsCoordinates palettes combined