Code: Select all
#Include Once "windows.bi"
#Include Once "vbcompat.bi"
#Include Once "win/Ole2.bi"
#Include Once "crt/math.bi"
Type Vector
Public:
Declare Constructor()
Declare Constructor (Size As Integer,value As Double=0#)
Declare Constructor (value() As Double)
Declare Constructor (ByRef value As Vector)
Declare Destructor
Declare Property Size() As Integer
Declare Property Lenght() As Integer
Declare Operator [](index As Integer)ByRef As Double
Declare Function at(index As Integer)ByRef As Double
Declare Operator Let(v As vector)
Declare Operator Let(ByVal value As Double)
Declare Function toString(ivertically As Integer=0)As String
Declare Operator Cast() As String
Private:
ReDim Arr(0 To -1) As Double
As Integer m_lowbound=1
As Integer m_Highbound
End Type
Constructor Vector()
End Constructor
Constructor Vector(_Size As Integer,value As Double=0#)
m_Highbound=_Size
ReDim Arr(1 To m_Highbound)
If _Size Then
For i As Integer= 1 To _Size
Arr(i)=value
Next
End If
End Constructor
Constructor Vector( value() As Double)
m_Highbound=UBound(value)
ReDim Arr(1 To UBound(value))
If UBound(value) Then
For i As Integer= 1 To UBound(value)
Arr(i)=value(i)
Next
End If
End Constructor
Constructor Vector(ByRef value As Vector)
this.constructor(value.Size )
If value.Size Then
For i As Integer= 1 To value.Size
Arr(i)=value[i]
Next
End If
End Constructor
Destructor Vector
Erase Arr
End Destructor
Property Vector.Size() As Integer
Return UBound(Arr)-LBound(Arr)+1
End Property
Property Vector.Lenght() As Integer
Return UBound(Arr)-LBound(Arr)+1
End Property
Operator Vector.[](index As Integer)ByRef As Double
If index<1 Or index>this.Size Then
Messagebox NULL , "L'indice est en dehors de la plage (" & index & ")" ,"erreur dans Vector.[]",MB_ICONERROR
Exit Operator
EndIf
Return Arr(index)
End Operator
Function Vector.at(index As Integer) ByRef As Double
If index<1 Or index>this.Size Then
Messagebox NULL , "L'indice est en dehors de la plage (" & index & ")" ,"erreur dans Vector.at",MB_ICONERROR
Exit Function
EndIf
Return Arr(index)
End Function
Operator Vector.Let(v As vector)
ReDim Arr(1 To v.Size)
For i As Integer= 1 To v.Size
Arr(i)=v[i]
Next
End Operator
Operator Vector.Let(byval value As Double)
If Size=0 Then Return
For i As Integer= LBound(Arr) To UBound(Arr)
Arr(i)=value
Next
End Operator
Function Vector.toString(ivertically As Integer=0) As String
If Size=0 Then Return "Is empty"
Dim as String sOut
If ivertically=0 Then
for i As Integer= LBound(Arr,1) To UBound(Arr,1)
sout+=(Format(Arr(i),"####0.00")+ " ")
Next i
Else
For i As Integer= LBound(Arr,1) To UBound(Arr,1)
sout+= Format(Arr(i),"####0.00")+ Chr(13,10)
Next i
End If
return sout
End Function
Operator Vector.Cast() As String
Return this.toString
End Operator
Operator + (ByRef v1 As Vector,ByRef v2 As Vector) As Vector
Dim v As Vector=vector(Max(v1.Size,v2.Size))
For i As Integer=1 To Max(v1.Size,v2.Size)
v[i]=v1[i]+v2[i]
Next
Return v
End Operator
Operator +( d As Double, v As Vector) As Vector
Dim vv As Vector=Vector(v)
For i As Integer= 1 To v.Size
vv[i]=v[i]+d
Next
Return vv
End Operator
Operator - (ByRef v1 As Vector,ByRef v2 As Vector) As Vector
Dim v As Vector=vector(Max(v1.Size,v2.Size))
For i As Integer=1 To Max(v1.Size,v2.Size)
v[i]=v1[i]-v2[i]
Next
Return v
End Operator
Operator -( d As Double, v As Vector) As Vector
Return (-d)+v
End Operator
Operator * (ByRef v1 As Vector,ByRef v2 As Vector) As Vector
Dim v As Vector=vector( Max(v1.Size,v2.Size))
For i As Integer=1 To Max(v1.Size,v2.Size)
v[i]=v1[i]*v2[i]
Next
Return v
End Operator
Operator * ( n As Integer, v As Vector) As Vector
Dim vv As Vector=Vector(v)
For i As Integer= 1 To v.Size
vv[i]=v[i]*n
Next
Return vv
End Operator
Operator * ( v As Vector, n As Integer) As Vector
Return n*v
End Operator
Operator - ( v As Vector) As Vector
Dim vv As Vector=Vector(v)
For i As Integer= 1 To v.Size
vv[i]=-v[i]
Next
Return vv
End Operator
Operator ^ ( v1 As Vector, v2 As Vector) As Vector
Return v1*v2
End Operator
Operator ^ ( v As Vector, d As Double) As Vector
Dim vv As Vector=Vector(v)
For i As Integer= 1 To v.Size
vv[i]= v[i]^d
Next
Return vv
End Operator
Operator =(v1 As Vector,v2 As Vector) As BOOL
Dim flag As BOOL
If v1.Size=v2.Size Then
For i As Integer= 1 To v1.Size
If v1[i] <> v2[i] Then
Return FALSE
Else
flag=CTRUE
EndIf
Next
EndIf
Return flag
End Operator
Operator =(v As Vector,value As Double) As BOOL
For i As Integer= 1 To v.Size
If v[i] <> value Then
Return FALSE
EndIf
Next
Return CTRUE
End Operator
Function ArrayToVector(a() As Double) As Vector
Dim v As Vector=Vector(UBound(a))
For i As Integer= 1 To UBound(a)
v[i]=a(i)
Next
Return v
End Function
Function SORT (a As Vector,ASCEND_direction As BOOL=CTRUE ) As Vector
Dim As integer i
Dim As integer j
Dim As Double s
Dim res As Vector=a
If ASCEND_direction Then ASCEND_direction=1
Select Case ASCEND_direction
Case 1
For i = 1 To res.Size
For j = i+1 To res.Size
if (res[j]< res[i]) Then
s=res[i]
res[i]=res[j]
res[j]=s
end if
Next j
Next i
Case 0
For i = 1 To res.Size
For j = i+1 To res.Size
if ( res[j] > res[i]) Then
s=res[i]
res[i]=res[j]
res[j]=s
end if
Next j
Next i
End Select
Return res
End Function
Code: Select all
#Include Once "vector.bi"
Type Matrix
Public:
Declare Constructor()
Declare Constructor (Highbound1 As Integer,Highbound2 As Integer)
Declare Destructor
Declare Property Rows(value As Integer) ' set the number of rows
Declare Property Rows() As Integer ' returns the number of rows
Declare Property Cols(value As Integer) ' set the number of columns
Declare Property Cols() As Integer ' returns the number of columns
Declare Operator [](irow As integer)ByRef As vector
Declare Function at(irow As Integer,icol As Integer) ByRef As Double
Declare Operator Let(ByRef m As Matrix)
Declare Operator Let(byval value As Double)
Declare Operator Cast() As String
Private:
ReDim Arr(0 To -1) As Vector
m_lowbound(1 To 2) As Integer
m_Highbound(1 To 2)As Integer
End Type
Constructor Matrix()
ReDim Arr(0 To -1)
End Constructor
Destructor Matrix()
Erase Arr
End Destructor
Constructor Matrix (Highbound1 As Integer,Highbound2 As Integer)
ReDim Arr(1 To Highbound1)
For i As Integer=1 To Highbound1
Arr(i)=vector(Highbound2)
Next i
m_lowbound(1)=1 :m_lowbound(2)=1
m_Highbound(1)=Highbound1: m_Highbound(2)=Highbound2
End Constructor
Property Matrix.Rows(value As Integer) ' set the number of rows
m_Highbound(1)=value ': m_Highbound(2)=this.Cols sinon
ReDim Arr(1 To m_Highbound(1))
For i As Integer=1 To m_Highbound(1)
Arr(i)=vector(m_Highbound(2))
Next i
End Property
Property Matrix.Rows() As Integer ' returns the number of rows
return UBound(Arr,1)-LBound(Arr,1) +1
End Property
Property Matrix.Cols(value As Integer) ' set the number of columns
m_Highbound(2)=value ':m_Highbound(1)=this.Rows: sinon erreur
ReDim Arr(1 To m_Highbound(1))
For i As Integer=1 To m_Highbound(1)
Arr(i)=vector(m_Highbound(2))
Next i
End Property
Property Matrix.Cols() As Integer ' returns the number of columns
return m_Highbound(2)-LBound(Arr,1) +1
End Property
Operator Matrix.[](irow As integer)ByRef As vector
Return Arr(irow)
End Operator
Function Matrix.at(irow As Integer,icol As Integer) ByRef As Double
If irow<1 Or irow>m_Highbound(1) _
Or icol<1 Or icol>m_Highbound(2) Then
Messagebox NULL , "Les indices sont en dehors de la plage (" & irow & ";" & icol & ")" ,"erreur dans Matrix.at",MB_ICONERROR
Exit Function
EndIf
Return Arr(irow)[icol]
End Function
Operator Matrix.Let(ByRef m As Matrix)
if(@this <> @m) Then 'assignation M = M impossible
This.constructor( m.m_Highbound(1), m.m_Highbound(2))
For i As Integer= 1 To UBound(Arr,1)
For j As Integer= 1 To Arr(i).Size
Arr(i)[j]=m.at(i,j)
Next
Next
End If
End Operator
Operator Matrix.Let(byval value As Double)
For i As Integer= 1 To UBound(Arr,1)
For j As Integer= 1 To Arr(i).Size
Arr(i)[j]=value
Next
Next
End Operator
Operator Matrix.cast() As String
Dim as String sOut
For i As Integer= 1 To UBound(Arr,1)
For j As Integer= 1 To Arr(i).Size
Dim as Double value = Arr(i)[j]
sout +=" "
sout+=(" " + Format(value,"####0.0000"))
Next j
sOut += (!"\n")
Next i
Return sout
End Operator
' write differents operators heare
Function Eye OverLoad (ByVal n As Integer) As Matrix ' Creates a new Identity matrix.
Dim As Matrix buf = Matrix(n, n)
for i As Integer = 1 To n
For j As Integer = 1 To n
If i=j Then buf.at(i,j) = 1.0
Next j
Next i
Return buf
End Function
Function Eye OverLoad (ByVal n As Integer,ByVal m As Integer) As Matrix ' Creates a new Identity matrix.
Dim As Matrix buf = Matrix(n, m)
for i As Integer = 1 To n
For j As Integer = 1 To m
If i=j Then buf.at(i,j) = 1.0
Next j
Next i
Return buf
End Function
Function Zeros OverLoad (ByVal m As Integer, ByVal n As Integer) As Matrix ' Creates row by n matrix filled with zeros.
Return Matrix(m, n)
End Function
Public Function Zeros(ByVal n As Integer) As Matrix ' Creates n by n matrix filled with zeros.
Return Matrix(n,n)
End Function
Function Ones OverLoad (ByVal m As Integer, ByVal n As Integer) As Matrix ' Creates m by n matrix filled with ones.
Dim As Matrix buf = Matrix( m,n)
for i As Integer = 1 To m
For j As Integer = 1 To n
buf.at(i,j) = 1.0
Next j
Next i
Return buf
End Function
Function Ones(ByVal n As Integer) As Matrix ' Creates n by n matrix filled with ones.
Dim As Matrix buf = Matrix(n,n)
for i As Integer = 1 To n
For j As Integer = 1 To n
buf.at(i,j) = 1.0
Next j
Next i
Return buf
End Function
Function Transpose(m As Matrix)As Matrix
Dim t As Matrix=Matrix(m.cols,m.rows)
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
t[i][j]=m[j][i]
Next j
Next i
Return t
End Function
Operator + (m1 As Matrix,m2 As Matrix) As Matrix
Dim As Matrix m=Matrix(m1)
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
m.at(i,j)=m1.at(i,j)+ m2.at(i,j)
Next
Next
Return m
End Operator
Operator + (m1 As Matrix,d As Double) As Matrix
Dim As Matrix m=Matrix(m1)
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
m.at(i,j)=m1.at(i,j)+d
Next
Next
Return m
End Operator
Operator + (d As Double ,m1 As Matrix) As Matrix
Return (m1+d)
End Operator
Operator - (m1 As Matrix,m2 As Matrix) As Matrix ' subtraction of Matrix with Matrix
Dim As Matrix m=Matrix(m1)
' check if the dimensions match
If (m1.rows = m2.rows And m1.cols = m2.cols) then
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
m[i][j]=m1[i][j]-m2[i][j]
Next
Next
End If
Return m
End Operator
Operator - (m As Matrix) As Matrix ' minus of Matrix with Matrix
Dim As Matrix m2=Matrix(m)
For i As Integer= 1 To m2.Rows
For j As Integer= 1 To m2.Cols
m2[i][j]= -m[i][j]
Next
Next
Return m2
End Operator
Operator - (d As Double,m2 As Matrix) As Matrix
Dim As Matrix m=Matrix(m2)
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
m.at(i,j)= d-m2.at(i,j)
Next
Next
Return m
End Operator
Operator - (m2 As Matrix,d As Double) As Matrix
Return (-d+m2)
End Operator
Private Function matmul OverLoad (m1 As Matrix,m2 As Matrix) As Matrix
If (M1.cols <> M2.rows)Then
Messagebox Getactivewindow(),"Revoir les bornes des Matrix m1 et m2","Matmul impossible",MB_ICONERROR
Return Matrix()
EndIf
Dim m3 As Matrix=Matrix( m1.Rows, m2.Cols)
Dim As Double DSUM
For I As Integer=1 to m1.Rows
For J As Integer=1 to m2.Cols
DSUM=0.00
For K As Integer=1 to m1.Cols
DSUM=DSUM+m1.at(I,K)*m2.at(K,J)
Next K
m3.at(I,J)=DSUM
Next j
Next i
Return m3
End Function
Operator * (A As Matrix,B As Matrix) As Matrix
Return matmul(A,B)
End Operator
Operator * (m1 As Matrix,v As vector) As Vector 'Y(M)=A(M,N)*X(N)
If (m1.cols<> v.size) Then
Messagebox Getactivewindow(),"Revoir les bornes de la Matrix m1 et du vecteur","Multiplication matrice-vecteur impossible",MB_ICONERROR
Return Vector()
EndIf
Dim Y As Vector=Vector(m1.Rows)
Dim As Double dSum=0.0D+00
For i As Integer =1 To m1.Rows
dSum = 0.0D+00
For j As Integer = 1 To m1.cols
dSum = dSum + m1.at(i,j) * v[j]
Y[i]=dSum
Next j
Next i
Return Y
End Operator
Operator * (v As vector,m2 As Matrix) As Vector
Return m2*v
End Operator
Operator * (n As Integer,m As Matrix) As Matrix
Dim As Matrix nm=Matrix(m)
For i As Integer= 1 To m.rows
For j As Integer= 1 To m.cols
nm.at(i,j)=n*m.at(i,j)
Next
Next
Return nm
End Operator
Operator * (m As Matrix,n As Integer) As Matrix
Return (n*m)
End Operator
Operator ^ (m As Matrix, n As Integer) As Matrix
' If n=-1 Then Return m.inverse()
If n=0 Then Return Eye(m.rows,m.cols)
If n<0 Then
Messagebox (Getactivewindow(),"n<0 Pas encore pris en charge","Error",MB_ICONERROR)
Return Matrix()
EndIf
Dim As Matrix mm=Matrix(m)
For i As Integer=1 To n
mm = mm*mm
Next i
Return mm
End Operator
Operator \(m1 As Matrix,Y As vector) As Vector 'résolution de gauss A(M,N)*X(N)=Y(M)
Dim X As Vector=Y
' m1.LINEQ(X)
Return X
End Operator
Operator =(m1 As Matrix,m2 As Matrix) As BOOL
Dim flag As BOOL
If m1.Rows=m2.Rows And _
m1.cols =m2.cols Then
For i As Integer= 1 To m1.Rows
For j As Integer= 1 To m1.cols
If m1.at(i,j) <> m2.at(i,j) Then
Return FALSE
Else
flag=CTRUE
EndIf
Next
Next
EndIf
Return flag
End Operator
Operator =(m As Matrix,value As Double) As BOOL
For i As Integer= 1 To m.Rows
For j As Integer= 1 To m.Cols
If m.at(i,j) <> value Then
Return FALSE
EndIf
Next
Next
Return CTRUE
End Operator
test1.bas
Code: Select all
#Include Once "windows.bi"
#Include Once "vbcompat.bi"
#Include Once "Matrix.bas"
#Include Once "crt.bi"
Print " MATRICES SANS CONSTRUCTORS"
Dim M1 As Matrix '=Matrix(1,3)
M1.Rows=1 :M1.cols=3
M1.at(1,1)=1.00 : M1.at(1,2)=2.00 : M1.at(1,3)=3.00
Dim M2 As Matrix '=Matrix(3,1)
M2.Rows=3 :M2.cols=1
M2.at(1,1)=0.00
M2.at(2,1)= -1.00
M2.at(3,1)=2.00
Dim M As Matrix
Print "M1:"
Print M1
Print
Print "M2:"
Print M2
Print
Print
Print "M1*M2: possible si M1.cols = M2.rows"
M= M1*M2
Print M
Print "M.rows= " & M.rows
Print "M.Cols= " & M.Cols
Print
Print "M2*M1:"
M= M2*M1
Print M
Print "M.rows= " & M.rows
Print "M.Cols= " & M.Cols
Print
Dim A As Matrix
A.Rows=2 : A.Cols=3
A.at(1,1)=0.00 : a.at(1,2)=0.00 : A.at(1,3)=0.00
A.at(2,1)=1.00 : A.at(2,2)=2.00: A.at(2,3)=3.00
Dim B As Matrix '=Matrix(3,3)
B.rows=3 :B.cols=3
B.at(1,1)=-1.00 : B.at(1,2)=1.00 : B.at(1,3)=0.00
B.at(2,1)= -1.00 : B.at(2,2)=1.00 : B.at(2,3)=-3.00
B.at(3,1)=1.00 : B.at(3,2)=-1.00 : B.at(3,3)=2.00
Print "A:"
Print A
Print
Print "B:"
Print B
Print
Print "A*B can be null with A and B not null"
Print "A*B:"
Print A*B ' possible si A.cols = B.rows
Print
Sleep