MonkeeSage wrote:
/ …
Anyhow, I don’t really care if []= is there by default, I can add it if
I want it; but I just don’t understand why “a matrix [as a value] is
immutable” means that []= shouldn’t be there regarding the data
structure.
You might as well ask why there is no formal method to change the third
digit of an integer in class Fixnum. The reply would be that integers
are
ordinarily dealt with in their entirety, and for special needs, the user
of
the class can write custom code to deal with the border cases, but
there’s
no point in trying to include all the marginal utilities in the formal
class definition.
Most matrix operations operate on matrices as units. Here’s an example,
a
matrix from my library, responsible for rotating objects in three-space:
class RotationMatrix
ToRad = Math::PI / 180.0
ToDeg = 180.0 / Math::PI
perspective depth cue for 3D -> 2D transformation
PerspectiveDepth = 10.0
angular constant for anaglyphic rotation
AnaglyphScale = 0.03
populate 3D matrix with values for x,y,z rotations
def populate_matrix(xa,ya,za)
# create trig values
sx = Math.sin(xa * ToRad);
cx = Math.cos(xa * ToRad);
sy = Math.sin(ya * ToRad);
cy = Math.cos(ya * ToRad);
sz = Math.sin(za * ToRad);
cz = Math.cos(za * ToRad);
# build rotation matrix
@rot_mat =
[
[
cy * cx - sy * sx * sz,
- cy * sx - cx * sy * sz,
cz * sy
],
[
sx * cz,
cz * cx,
sz
],
[
- cx * sy - sx * sz * cy,
sx * sy - sz * cx * cy,
cz * cy
]
]
end
add perspective to 2D points
and perform anaglyph position change if specified
def convert_3d_to_2d(x,y,z,anaglyph_flag)
x = (x * (PerspectiveDepth + z))/PerspectiveDepth
x += z * anaglyph_flag * AnaglyphScale if anaglyph_flag
y = (y * (PerspectiveDepth + z))/PerspectiveDepth
return x,y
end
rotate a 3D point using matrix values
then project into 2D space
def rotate_convert(v,anaglyph_flag)
z = @rot_mat[0][0] * v.z + @rot_mat[0][1] * v.y + @rot_mat[0][2] *
v.x
y = @rot_mat[1][0] * v.z + @rot_mat[1][1] * v.y + @rot_mat[1][2] *
v.x
x = @rot_mat[2][0] * v.z + @rot_mat[2][1] * v.y + @rot_mat[2][2] *
v.x
x,y = convert_3d_to_2d(x,y,z,anaglyph_flag)
return x,y
end
end
In this case, the matrix at the heart of the class is the most
economical
way to accomplish the mathematical objective, and there’s really no
point
in being able to change any of its constituent parts, once it has been
constructed. It would be very easy to modify any single array cell, but
there’s simply no point.
I think the writers of the Matrix class realized this, and didn’t bother
to
include an assignment operator on the ground that it wouldn’t have any
general utility.