John Fredsted

2238 Reputation

15 Badges

20 years, 164 days

MaplePrimes Activity


These are Posts that have been published by John Fredsted

Consider the following two codes: Code 1:
restart:
with(LinearAlgebra):
M1,M2 := Vector(2,fill=Matrix(4,4))$2:
M1[1] := M1[1] + IdentityMatrix(4):
M2[1] := M2[1] + IdentityMatrix(4):
M1[1];
Code 2:
restart:
with(LinearAlgebra):
M1,M2 := Vector(2,fill=Matrix(4,4)),Vector(2,fill=Matrix(4,4)):
M1[1] := M1[1] + IdentityMatrix(4):
M2[1] := M2[1] + IdentityMatrix(4):
M1[1];
According to my knowledge they ought to give identical output: the 4-dimensional identity matrix. But they don't. The first code gives twice that, as if M1[1] and M2[1] reference the same object in memory. The second code gives the correct answer.
A note added: Although the coding below is correct, it has clearly been superseeded by the following two entries contributed by acer: Entry 1 and Entry 2. As in any fairytale (even though this blog of mine certainly is not) it takes three of something (at least according to the fairytales of my fellow-countryman H. C. Andersen). Todays entry is the third and last (at least for now) in a row of three consecutive ones dealing with manipulations of indices of Arrays. The other two entries are Tip: Index an Array and Tip: Permute the indices of an Array.
A note added: Although the coding below is correct, it has clearly been superseeded by the following two entries contributed by acer: Entry 1 and Entry 2 in the blog entry Tip: Transpose a pair of indices of an Array. Yesterday I wrote about a method to index an Array, using a procedure of the type `index/method`. Below, using the same sort of procedure, a method for permuting the indices of an Array is given (please feel free to suggest improvements; probably, the else-statement may be written more concisely):
Sometimes it can be very useful to know the indices of entries (of an Array) which obey some conditions. The following procedure (which works for any Array) makes this possible:
`index/makeIndex` := proc(indices::list,array::Array,value::list)
	# Retrieving from the Array
	if nargs = 2 then return array[op(indices)]: end if:
	# Storing in the Array
	if nargs = 3 then array[op(indices)] := indices = op(value): end if:
end proc:
An example: For Array A find the set S of indices of entries being positive integers, using as an intermediate step the Array B with entries of the form "indices = value":
First 9 10 11 12 Page 11 of 12