Working with Matrices

CMatrix:new


The new method creates an instance of the CMatrix class. This method returns to the caller a reference to the object it creates. The calling code must assign this reference to a name (see below). If the object cannot be created, nil is returned.

Note: This method's name uses all lower case to present it as the analog of the C++ new operator. Generally, the names of class methods begin with an uppercase letter but new and delete are different.

Syntax

M = CMatrix:new()

    Default constructor. The new CMatrix M has no elements.

M = CMatrix:new( CMatrix2 )

    This is a copy constructor. The new CMatrix M is initialized to the members of the CMatrix2 argument.

M = CMatrix:new( n, m, val )

    The new CMatrix M is initialized to the first n rows x m columns of value val.

    val may be any type of Lua value, but defaults to 0.0 if omitted.

Example

The first example shows the default constructor and the use of delete to free memory when the CMatrix will no longer be used by the script:

 

M = CMatrix:new()

-- create a new instance of M of the CMatrix class.

  ...

-- other uses of the class go here, between new and delete.

M:delete()

-- deletes the object and its associated memory.

The next example shows the use of all 3 constructors: default, copy, and initialization:

 

M1 = CMatrix:new(5,5,1.0)

-- initialization constructor

Printf("M1 = %lg", M1:Members())

-- result: M1 = 25

M2 = CMatrix:new(M1)

-- copy constructor

Printf("M2 = %lg", M2:Members())

-- result: M2 = 25

M3 = CMatrix:new()

-- default constructor

M3:Init(1 2,2.0)

-- set the first member

Printf("M3 = %lg", M:Members())

-- result: M3 = 1

Related Topics

CMatrix, Copy, delete