There is a very cryptic error message that happens when I try to make this:
class mat2 {
public:
import "common.ulb"
func mat2()
float out[2, 2]
endfunc
static func create()
float out[2, 2]
out[0, 0] = 1.0, out[1, 0] = 0.0
out[0, 1] = 0.0, out[1, 1] = 1.0
return
endfunc
static mat2 func clone(mat2 a)
float out[2, 2]
out[0, 0] = a[0, 0], out[1, 0] = a[1, 0]
out[0, 1] = a[0, 1], out[1, 1] = a[1, 1]
return out
endfunc
static mat2 func copy(mat2 &out, mat2 a)
out[0, 0] = a[0, 0], out[1, 0] = a[1, 0]
out[0, 1] = a[0, 1], out[1, 1] = a[1, 1]
return
endfunc
static mat2 func identity(mat2 &out)
out[0, 0] = 1, out[1, 0] = 0
out[0, 1] = 0, out[1, 1] = 1
return out
endfunc
static mat2 func set(mat2 &out, float m00, float m01, float m10, float m11)
out[0, 0] = m00, out[1, 0] = m01
out[0, 1] = m10, out[1, 1] = m11
return out
endfunc
static mat2 func transpose(mat2 &out, mat2 a)
if (out == a)
float a1 = a[1, 0]
out[1, 0] = a[0, 1]
out[0, 1] = a1
else
out[0, 0] = a[0, 0], out[1, 0] = a[0, 1]
out[0, 1] = a[1, 0], out[1, 1] = a[1, 1]
endif
return out
endfunc
static float func determinant(mat2 a)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
return a0*a3 - a1*a2
endfunc
static mat2 func adjoint(mat2 &out, mat2 a)
float a0 = a[0, 0]
out[0, 0] = a[1, 1], out[1, 0] = -a[1, 0]
out[0, 1] = -a[0, 1], out[1, 1] = a0
return out
endfunc
static mat2 func invert(mat2 &out, mat2 a)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float det = a0*a3 - a2*a1
if (!det)
return null
$define debug
print("Singular matrix: cannot be inverted")
$undef debug
endif
det = 1.0 / det
out[0, 0] = a3 * det, out[1, 0] = -a1 * det
out[0, 1] = -a2 * det, out[1, 1] = a0 * det
return out
endfunc
static mat2 func add(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0 + b0, out[1, 0] = a1 + b1
out[0, 1] = a2 + b2, out[1, 1] = a3 + b3
return out
endfunc
static mat2 func subtract(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0 - b0, out[1, 0] = a1 - b1
out[0, 1] = a2 - b2, out[1, 1] = a3 - b3
return out
endfunc
static mat2 func multiply(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0*b0 + a2*b1, out[1, 0] = a1*b0 + a3*b1
out[0, 1] = a0*b2 + a2*b3, out[1, 1] = a1*b2 + a3*b3
return out
endfunc
static mat2 func rotate(mat2 &out, mat2 a, float rad)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float s = sin(rad), float c = cos(rad)
out[0, 0] = a0*c + a2*s, out[1, 0] = a1*c + a3*s
out[0, 1] = a0*-s + a2*c, out[1, 1] = a1*-s + a3*c
return out
endfunc
; static mat2 func scale(mat2 &out, mat2 a, vec2 v)
; float a0 = a[0, 0], float a1 = a[1, 0]
; float a2 = a[0, 1], float a3 = a[1, 1]
;
; float v0 = v.m_Elements[0]
; float v1 = v.m_Elements[1]
;
; out[0, 0] = a0*v0, out[1, 0] = a1*v0
; out[0, 1] = a2*v1, out[1, 1] = a3*v1
; return out
; endfunc
static mat2 func fromRotation(mat2 &out, float rad)
float s = sin(rad), float c = cos(rad)
out[0, 0] = c, out[1, 0] = s
out[0, 1] = -s, out[1, 1] = c
return out
endfunc
; static mat2 func fromScaling(mat2 &out, vec2 v)
; out[0, 0] = v.m_Elements[0], out[1, 0] = 0
; out[0, 1] = 0, out[1, 1] = v.m_Elements[1]
; return out
; endfunc
static func str(mat2 a)
$define debug
print("mat2(", a[0, 0], ", ", a[1, 0], "; ", a[0, 1], ", ", a[1, 1], ")")
$undef debug
endfunc
static float func frob(mat2 a)
return sqrt(a[0, 0]*a[0, 0] + a[1, 0]*a[1, 0] + \
a[0, 1]*a[0, 1] + a[1, 1]*a[1, 1])
endfunc
static mat2 func LDU(mat2 L, mat2 D, mat2 U, mat2 a)
mat2 LDU_Matrix = new mat2.create()
L[2] = a[0, 1] / a[0, 0]
U.m_Elements[0] = a[0, 0]
U.m_Elements[1] = a[1, 0], U.m_Elements[3] = a[1, 1] - L.m_Elements[2]*U.m_Elements[1]
D.m_Elements[0] = U.m_Elements[0], D.m_Elements[3] = U.m_Elements[3]
LDU_Matrix.m_Elements[0] = L, LDU_Matrix.m_Elements[1] = D, LDU_Matrix.m_Elements[2] = U
return LDU_Matrix
endfunc
}
And I get an "Invalid array reference" at line 21, column 19... I swear I had declared a
as a mat2, so why isn't it working?
There is a very cryptic error message that happens when I try to make this:
````
class mat2 {
public:
import "common.ulb"
func mat2()
float out[2, 2]
endfunc
static func create()
float out[2, 2]
out[0, 0] = 1.0, out[1, 0] = 0.0
out[0, 1] = 0.0, out[1, 1] = 1.0
return
endfunc
static mat2 func clone(mat2 a)
float out[2, 2]
out[0, 0] = a[0, 0], out[1, 0] = a[1, 0]
out[0, 1] = a[0, 1], out[1, 1] = a[1, 1]
return out
endfunc
static mat2 func copy(mat2 &out, mat2 a)
out[0, 0] = a[0, 0], out[1, 0] = a[1, 0]
out[0, 1] = a[0, 1], out[1, 1] = a[1, 1]
return
endfunc
static mat2 func identity(mat2 &out)
out[0, 0] = 1, out[1, 0] = 0
out[0, 1] = 0, out[1, 1] = 1
return out
endfunc
static mat2 func set(mat2 &out, float m00, float m01, float m10, float m11)
out[0, 0] = m00, out[1, 0] = m01
out[0, 1] = m10, out[1, 1] = m11
return out
endfunc
static mat2 func transpose(mat2 &out, mat2 a)
if (out == a)
float a1 = a[1, 0]
out[1, 0] = a[0, 1]
out[0, 1] = a1
else
out[0, 0] = a[0, 0], out[1, 0] = a[0, 1]
out[0, 1] = a[1, 0], out[1, 1] = a[1, 1]
endif
return out
endfunc
static float func determinant(mat2 a)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
return a0*a3 - a1*a2
endfunc
static mat2 func adjoint(mat2 &out, mat2 a)
float a0 = a[0, 0]
out[0, 0] = a[1, 1], out[1, 0] = -a[1, 0]
out[0, 1] = -a[0, 1], out[1, 1] = a0
return out
endfunc
static mat2 func invert(mat2 &out, mat2 a)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float det = a0*a3 - a2*a1
if (!det)
return null
$define debug
print("Singular matrix: cannot be inverted")
$undef debug
endif
det = 1.0 / det
out[0, 0] = a3 * det, out[1, 0] = -a1 * det
out[0, 1] = -a2 * det, out[1, 1] = a0 * det
return out
endfunc
static mat2 func add(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0 + b0, out[1, 0] = a1 + b1
out[0, 1] = a2 + b2, out[1, 1] = a3 + b3
return out
endfunc
static mat2 func subtract(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0 - b0, out[1, 0] = a1 - b1
out[0, 1] = a2 - b2, out[1, 1] = a3 - b3
return out
endfunc
static mat2 func multiply(mat2 &out, mat2 a, mat2 b)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float b0 = b[0, 0], float b1 = b[1, 0]
float b2 = b[0, 1], float b3 = b[1, 1]
out[0, 0] = a0*b0 + a2*b1, out[1, 0] = a1*b0 + a3*b1
out[0, 1] = a0*b2 + a2*b3, out[1, 1] = a1*b2 + a3*b3
return out
endfunc
static mat2 func rotate(mat2 &out, mat2 a, float rad)
float a0 = a[0, 0], float a1 = a[1, 0]
float a2 = a[0, 1], float a3 = a[1, 1]
float s = sin(rad), float c = cos(rad)
out[0, 0] = a0*c + a2*s, out[1, 0] = a1*c + a3*s
out[0, 1] = a0*-s + a2*c, out[1, 1] = a1*-s + a3*c
return out
endfunc
; static mat2 func scale(mat2 &out, mat2 a, vec2 v)
; float a0 = a[0, 0], float a1 = a[1, 0]
; float a2 = a[0, 1], float a3 = a[1, 1]
;
; float v0 = v.m_Elements[0]
; float v1 = v.m_Elements[1]
;
; out[0, 0] = a0*v0, out[1, 0] = a1*v0
; out[0, 1] = a2*v1, out[1, 1] = a3*v1
; return out
; endfunc
static mat2 func fromRotation(mat2 &out, float rad)
float s = sin(rad), float c = cos(rad)
out[0, 0] = c, out[1, 0] = s
out[0, 1] = -s, out[1, 1] = c
return out
endfunc
; static mat2 func fromScaling(mat2 &out, vec2 v)
; out[0, 0] = v.m_Elements[0], out[1, 0] = 0
; out[0, 1] = 0, out[1, 1] = v.m_Elements[1]
; return out
; endfunc
static func str(mat2 a)
$define debug
print("mat2(", a[0, 0], ", ", a[1, 0], "; ", a[0, 1], ", ", a[1, 1], ")")
$undef debug
endfunc
static float func frob(mat2 a)
return sqrt(a[0, 0]*a[0, 0] + a[1, 0]*a[1, 0] + \
a[0, 1]*a[0, 1] + a[1, 1]*a[1, 1])
endfunc
static mat2 func LDU(mat2 L, mat2 D, mat2 U, mat2 a)
mat2 LDU_Matrix = new mat2.create()
L[2] = a[0, 1] / a[0, 0]
U.m_Elements[0] = a[0, 0]
U.m_Elements[1] = a[1, 0], U.m_Elements[3] = a[1, 1] - L.m_Elements[2]*U.m_Elements[1]
D.m_Elements[0] = U.m_Elements[0], D.m_Elements[3] = U.m_Elements[3]
LDU_Matrix.m_Elements[0] = L, LDU_Matrix.m_Elements[1] = D, LDU_Matrix.m_Elements[2] = U
return LDU_Matrix
endfunc
}
````
And I get an "Invalid array reference" at line 21, column 19... I swear I had declared `a` as a mat2, so why isn't it working?