MathGroup Archive 2005

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

Search the Archive

Re: Types in Mathematica, a practical example

  • To: mathgroup at
  • Subject: [mg62937] Re: [mg62800] Types in Mathematica, a practical example
  • From: "Ingolf Dahl" <ingolf.dahl at>
  • Date: Fri, 9 Dec 2005 05:10:16 -0500 (EST)
  • Reply-to: <ingolf.dahl at>
  • Sender: owner-wri-mathgroup at

Thanks for all answers. 
In my previous submission I asked for a way to define/declare list with
undefined elements. All the answers that I have got indicate that it is
difficult in the present versions of Mathematica. Maybe such a construction
would be awkward, definitely unnecessary and a way to see Mathematica from
the wrong point of view. 
But once upon a time "zero" was unknown as concept and symbol. Once, but
somewhat later, I did some programming in a language named ASYST (a dialect
of Forth as PostScript also is), where zero-length empty strings were not
allowed. This single design flaw I considered as the largest drawback of the
ASYST language. From my point of view I see these shortfalls as symptoms of
the same syndrome. 

I would not really enjoy having to explain to a newbie what error he had
done if she/he had encountered the same problem as I stated initially. I
would have to explain that in Mathematica it is necessary to choose between
two ways to define matrices and similar structures: Either from bottom and
up, e.g. by using Array, or from top and down, e.g. by using Table. In the
first case, if you give the whole structure a name, it is not possible to
use the same name as is used for assigning values to the individual

What I would prefer is a new Mathematica word: "Undefined". Then I could
define a list x as 
x = {1,Undefined,2};

x[[1]] should evaluate to 1 as usual, but for this word "Undefined" the list
part x[[2]] should evaluate to x[[2]], precisely as for an undefined
unindexed symbol.
x[[4]] should give the usual error message:

"Part::partw: Part 4 of {1, Undefined, 2} does not exist."

x[[3]] =. and Clear[x[[3]]] should redefine x as {1,Undefined,Undefined},
and x = Undefined should be interpreted as x =.

Could this be implemented by operator overloading or by unprotecting and
redefining Part?

I believe that this could be implemented in Mathematica without destroying
compatibility with existing code, since it would be an added-on feature. And
my intuition (if that is of any value) tells me that such an addition would
have benefits I fail to see. 

As newbie I was curious if I could do something according to the following
idea: Define a triangle by 

t = triangle[{3,4,Undefined}, {Undefined, Undefined, Pi/2}]

with a list of the sides as the first argument and a list of the angles as
the second. Then I wanted to write a function, which would give the
following output when applied to t:

In[]= AngleSumTheorem[t]

Out[]= {t[[2,1]]+t[[2,2}]]+Pi/2==Pi}

Then I also should be able to write LawOfSines and LawOfCosines functions,
solve the equations and feed back the values into t. Unfortunately I cannot
do this in Mathematica in this elegant way. Is this to demand too much from

In the answers I have got, there are some different work-arounds suggested
to my initial problem, to correct the following code (see my first letter

a = {{1, 2}, {3, 4}};
x - a /. {x -> a}

The first work-around is to use 

xx = Array[x, Dimensions[a]]

I know that this is a way to get around the problem in Mathematica, but it
is a clumsy way. I have to have two different entities in the air; x for
assignments of new values to the matrix, and xx as a short form for the
matrix, reading out the values. It is like "My name is Bill but call me
Tom". And a clumsy way leads the thinking astray. I have to choose every
time I want to define a matrix: should I use a simple variable definition or
a double notations as you suggest? It would be much better if the same
notation could be used for the matrix and the elements consistently, without
having to choose from the beginning if I am going to work with the entity or
with the elements.

The second work-around is to redefine or manipulate the operator that is
applied to the list or matrix I am working with. For matrices there is a
whole bunch of functions that has to be rewritten, and I do not find this
idea very attractive. I feel it as a round-about way to solve the problem.
Andrzej Kozlowski, who suggests such a solution, thinks that I am looking at
Mathematica from the wrong view point. It is my strong belief that one
should be able, especially when mathematics is involved, to observe from as
many different view points as possible. An item is not really beautiful, or
properly designed, if it is not beautiful from all view points. The same
should apply to Mathematica, and I have also understood that Mathematica was
intentionally designed to be well-behaved when seen from different view
points. Or?

Kristen W Carlson suggests that I should initialize the matrix to have zero
in all element. No,

a = {{1, 2}, {3, 4}};
x = {{0, 0}, {0, 0}};
x - a /. {x -> a}

will clearly not work for me. But a simple way to solve my initial problem
is of course to apply the operations in the right order: 

a = {{1, 2}, {3, 4}};
(x /. {x -> a}) - a

Best regards

Ingolf Dahl

-----Original Message-----
From: Ingolf Dahl [mailto:ingolf.dahl at] 
To: mathgroup at
Subject: [mg62937] [mg62800] Types in Mathematica, a practical example

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,

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

  • Prev by Date: Re: exponential diophantine equations
  • Next by Date: Re: Re: Re: Types in Mathematica
  • Previous by thread: Re: EUREKA Re: Types in Mathematica, a practical example
  • Next by thread: inequations