简体   繁体   English

如何在创建的Matrix类中保存x和y?

[英]How to save x and y in class Matrix i have created?

I have defined two objects X and Y both have same size array as an matrix 我定义了两个对象X和Y都具有与矩阵相同的大小数组

    x:= Matrix new.
    x
      rows: 2 columns: 2;
      row: 1 column: 1 put: 2;
      row: 2 column: 1 put: 2;
      row: 1 column: 2 put: 2;
      row: 2 column: 2 put: 2.
    #(2 2 2 2)  "The x returns an array"
    y := Matrix new
    y
     rows: 2 columns: 2;
     row: 1 column: 1 put: 2;
     row: 2 column: 1 put: 2;
      row: 1 column: 2 put: 2;
     row: 2 column: 2 put: 2.
    #(2 2 2 2) "The object y returns an array"

Notes: 笔记:

  • rows:columns is a method which gives the matrix rows and columns rows:columns是一种提供矩阵行和列的方法
  • row:column is method that puts value into the matrix. row:column是将值放入矩阵的方法。

So, you created a class Matrix. 因此,您创建了一个Matrix类。 It is similar to Array but specialized for matrix-like messages (the ones you used). 它与Array类似,但专门用于类似矩阵的消息(您使用的消息)。 Now you created two instances x and y of Matrix and put their entries using the messages you defined. 现在,您创建了Matrix的x和y的两个实例,并使用您定义的消息放置了它们的条目。 Everything is fine so far. 到目前为止一切都很好。

Now you want to "save" these instances, presumably to operate with them exercising other messages such as sum, multiplication, transposition, product by scalar, and so on. 现在,您要“保存”这些实例,大概是要与它们一起使用其他消息,例如求和,乘法,换位,标量乘积等。 Your question is "how do I save x and y?" 您的问题是“如何保存x和y?” The answer is: not in the class Matrix! 答案是: 不在Matrix类中! .

A good idea would be to create a subclass of TestCase, namely MatrixTest, and add there methods for testing such as testSum, testMultiplication, testScalarMultiplication, testTransposition, and so on. 一个好主意是创建TestCase的子类,即MatrixTest,并在其中添加用于测试的方法,例如testSum,testMultiplication,testScalarMultiplication,testTransposition等。 Move the code that creates x and y to these methods and have these instances of Matrix held in temporaries of the method. 将创建x和y的代码移到这些方法上,并将这些Matrix实例保存在该方法的临时实例中。 Something on the lines of: 在以下方面:

MatrixText >> testSum
| x y z |
x := Matrix new rows: 2 columns: 2.
x row: 1 column: 1 put: 2.
x row: 1 column: 2 put: 2.
"<etc>"
y := Matrix new rows: 2 columns: 2.
y row: 1 column: 1 put: 2.
"<etc>"
z = x + y (you need to define the method + in Matrix!).
self assert: (z row: 1 column: 1) = 4.
"<etc>"

Generally speaking, you will not save instances of Matrix in Matrix, but in other classes that use matrices. 一般来说,您不会将Matrix的实例保存在Matrix中,而是保存在其他使用矩阵的类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算具有x和y值的矩阵中的位置 - How can i calculate position in a matrix with x and y values 如何检索坐标x,y已保存在另一个矩阵中的特定行? - How to retrieve specific rows that the coordinates x,y have been saved in another matrix? 如何根据值 X 和 Y 填充矩阵 - How to Fill a Matrix based on values X and Y 如何从LH 4x4世界矩阵中获取位置和x / y / z轴? - How do I get position and x/y/z axis out of a LH 4x4 world matrix? 如何反转4x4矩阵的y坐标? - How to invert the y coordinate of a 4x4 matrix? MATLAB:如何使用带有矩阵输入的plot()指定线属性? 即plot([x1 x2 x3],[y1 y2 y3]) - MATLAB: How Do I Specify Line Properties using plot() w/ Matrix Input? i.e. plot([x1 x2 x3],[y1 y2 y3]) 如何将 f(x,y) 转换为二维矩阵(即查找表)? - How can I convert f(x,y) to a 2-D matrix (i.e. lookup table)? 如何将x,y,z坐标输入为矩阵内的单个元素? - How do I input x,y,z co-ordinates as a single element inside a matrix? 如何获得轴坐标中的x和y值而不是矩阵位置? - How can I get x and y value in axis coordinates instead of matrix position? 如何在Matrix3d上获得比例尺(x,y,z)和旋转量(x,y,z)? - How to get the scale(x,y,z) together with rotation(x,y,z) on a matrix3d?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM