MathGroup Archive 2005

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

Search the Archive

Re: Types in Mathematica, a practical example

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62835] Re: [mg62800] Types in Mathematica, a practical example
  • From: Pratik Desai <pdesai1 at umbc.edu>
  • Date: Tue, 6 Dec 2005 00:05:04 -0500 (EST)
  • References: <200512051841.NAA21133@smc.vnet.net> <4394B78F.4050809@umbc.edu> <4394C2F2.2030305@umbc.edu>
  • Sender: owner-wri-mathgroup at wolfram.com

Pratik Desai wrote:

> Pratik Desai wrote:
>
>> Ingolf Dahl wrote:
>>
>>> To MathGroup,
>>>
>>> I am not an advocate for strong typing in Mathematica, but consider the
>>> following simple example: I want to see if two matrices are equal. 
>>> One of
>>> them was the result from some equation, and is given inside a rule. 
>>> Then I
>>> write some code similar to this:
>>>
>>>
>>>
>>> a = {{1, 2}, {3, 4}};
>>>
>>> x - a /. {x -> a}
>>>
>>>
>>>
>>> I of course hope to get a matrix filled by zeroes, but if x is 
>>> undefined,
>>> the following is returned:
>>>
>>>
>>>
>>> {{{{0, 1}, {2, 3}}, {{-1, 0}, {1, 2}}}, {{{-2, -1}, {0, 1}}, {{-3, 
>>> -2}, {-1,
>>> 0}}}}
>>>
>>>
>>>
>>> First x was assumed to be a number, and (x - a) was evaluated. Then 
>>> x was
>>> substituted by the matrix a. No bug in Mathematica, but it was not 
>>> what I
>>> wanted as user. It is easy to make such a mistake in the 
>>> programming. Of
>>> course there are many ways to get around this problem, but is there any
>>> reasonably simple way to "type" x to be a list of lists without 
>>> specifying
>>> the elements, in such a way that the above example works?
>>>
>>>
>>>
>>> I could do
>>>
>>>
>>>
>>> ReleaseHold[Hold[x - a] /. {x -> a}]
>>>
>>>
>>>
>>> but then we are not in the "typing business" any longer.
>>>
>>>
>>>
>>> I think this question illuminates one aspect of the typing issue in
>>> Mathematica. I remember that I as a newbie looked for ways to declare
>>> matrices, in such a way that I later could specify matrix elements
>>> one-by-one, without initializing them first. I soon learned that 
>>> there are
>>> other ways to achieve similar results, but still I do not see any good
>>> reason why I cannot force Mathematica to give the following response 
>>> from
>>> x-a, if x in some way is declared to be a 2x2 list of lists:
>>>
>>>
>>>
>>> {{x[[1,1]] - 1, x[[1,2]] - 2},{x[[2,1]] - 3, x[[2,2]] - 4}}
>>>
>>>
>>>
>>> I am not allowed to Unset or Clear any part of a list either. Why not?
>>>
>>>
>>>
>>> Ingolf Dahl
>>>
>>> Sweden
>>>
>>>
>>>  
>>>
>> Hi Ingolf
>> Perhaps I am missing the point, and after reading some of the posts 
>> in this thread I am not really sure what type actually means. 
>> Nevertheless based on the definition
>> http://mathworld.wolfram.com/MatrixEquality.html
>> Why not write a function such as this??
>>
>> MatEQ[m_?MatrixQ, n_?MatrixQ] := (Flatten[m] === Flatten[n])
>>
>>
>>
>>
>>
>>
> Whoops, that will not work for obvious reasons
> s1 = {{1, 2, 3}, {4, 5, 6}}
> s2 = {{1, 2}, {3, 4}, {5, 6}}
> MatEQ[s1,s2]
> >>True
>
> :-[
>
>
>
But this might
MatEQ[m_?MatrixQ, n_?MatrixQ] := (
    Dimensions[m] === Dimensions[n]) && (Flatten[m] === Flatten[n])
s1 = {{1, 2, 3}, {4, 5, 6}}
s2 = {{1, 2}, {3, 4}, {5, 6}}
s3 = {{1, 2, 3}, {4, 5, 6}}
MatEQ[s1,s2]
 >>False
MatEQ[s1,s3]
 >>True



  • Prev by Date: Re: Types in Mathematica, a practical example
  • Next by Date: Re: Types in Mathematica, a practical example
  • Previous by thread: Re: Types in Mathematica, a practical example
  • Next by thread: Re: Types in Mathematica, a practical example