No
Yes.
Code: Select all
Type Vector
Public:
Declare Constructor()
Declare Constructor(Byval n As Uinteger)
Declare Operator [](Byval n As Uinteger) Byref As Double
Declare Property UpperBound() As Integer
Private:
Dim As Double array(Any)
End Type
Constructor Vector()
End Constructor
Constructor Vector(Byval n As Uinteger)
If n > 0 Then
Redim This.array(n - 1)
End If
End Constructor
Operator Vector.[](Byval n As Uinteger) Byref As Double
Return This.array(n)
End Operator
Property Vector.UpperBound() As Integer
Return Ubound(This.array)
End Property
Type Matrix
Public:
Declare Constructor()
Declare Constructor(Byval n1 As Uinteger, Byval n2 As Uinteger)
Declare Operator [](Byval n As Uinteger) Byref As Vector
Declare Property UpperBound1() As Integer
Declare Property UpperBound2() As Integer
Private:
Dim As Vector array(Any)
End Type
Constructor Matrix()
End Constructor
Constructor Matrix(Byval n1 As Uinteger, Byval n2 As Uinteger)
If n1 > 0 And n2 > 0 Then
Redim This.array(n1 - 1)
For i As Integer = 0 To n1 - 1
This.array(i) = Vector(n2)
Next I
End If
End Constructor
Operator Matrix.[](Byval n As Uinteger) Byref As Vector
Return This.array(n)
End Operator
Property Matrix.UpperBound1() As Integer
Return Ubound(This.array)
End Property
Property Matrix.UpperBound2() As Integer
If Ubound(This.array) >= 0 Then
Return This.array(0).UpperBound
Else
Return -1
End If
End Property
Operator *(Byref M As Matrix, Byref V As Vector) As Vector
If M.UpperBound2 = V.UpperBound Then
Dim As Vector R = Vector(M.UpperBound1 + 1)
For i As Integer = 0 To M.UpperBound1
For k As Integer = 0 To V.UpperBound
R[i] += M[i][k] * V[k]
Next k
Next i
Return R
Else
Error 1
End If
End Operator
Operator *(Byref M1 As Matrix, Byref M2 As Matrix) As Matrix
If M1.UpperBound2 = M2.UpperBound1 Then
Dim As Matrix R = Matrix(M1.UpperBound1 + 1, M2.UpperBound2 + 1)
For i As Integer = 0 To M1.UpperBound1
For j As Integer = 0 To M2.UpperBound2
For k As Integer = 0 To M1.UpperBound2
R[i][j] += M1[i][k] * M2[k][j]
Next k
Next j
Next i
Return R
Else
Error 1
End If
End Operator
'-----------------------------------------------------------------------------
Dim As Vector V = Vector(3)
V[0] = 1
V[1] = 2
V[2] = 3
For i As Integer = 0 To V.UpperBound
Print V[i]
Next i
Print
Dim As Matrix M1 = Matrix(3, 2)
M1[0][0]= 1 : M1[0][1] = 2
M1[1][0]= 3 : M1[1][1] = 4
M1[2][0]= 5 : M1[2][1] = 6
For i As Integer = 0 To M1.UpperBound1
For j As Integer = 0 To M1.UpperBound2
Print M1[i][j]; " ";
Next j
Print
Next i
Print
Dim As Matrix M2 = Matrix(2, 3)
M2[0][0]= 1 : M2[0][1] = 2 : M2[0][2] = 3
M2[1][0]= 4 : M2[1][1] = 5 : M2[1][2] = 6
For i As Integer = 0 To M2.UpperBound1
For j As Integer = 0 To M2.UpperBound2
Print M2[i][j]; " ";
Next j
Print
Next i
Print
Dim As Matrix Mx = M1 * M2
For i As Integer = 0 To Mx.UpperBound1
For j As Integer = 0 To Mx.UpperBound2
Print Mx[i][j]; " ";
Next j
Print
Next i
Print
Dim As Vector Vx = Mx * V
For i As Integer = 0 To Vx.UpperBound
Print Vx[i]
Next i
Print
Sleep
Code: Select all
'
' matx_smv.bas
'
' Successive matrix vector multiplications .
'
'' compile with: fbc matx_test.bas
#include once "easy_matx.bi"
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
' Matrix of matrices
'
dim as Matrix z(1 to lms)
'
' Vector of vectors
'
dim as Vector u(0 to lms)
'
' Dimension matrices and vectors .
'
for i=0 to lms-1
nx=m_seq(i)
ny=m_seq(i+1)
z(i+1) = Matrix(ny , nx)
u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
next i
'
' ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
print " Input vector, a matrix in another version ? "
print
prtv_v(u(), 0)
'
print " matrix of matrices values "
print
'
for i=1 to lms
gen_z(z() , i)
prt_z(z() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(i) = z(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms
print " z(";i;") "
prt_z(z() , i)
u(i) = z(i)*u(i-1)
print " u(";i;") "
prtv_v(u(), i)
next i
' end command acts as global destructor
' of allocated memory ?
end
'
' ======================================================================
'
Code: Select all
'
' matx_lrg.bas
'
' Successive matrix vector multiplications .
'
'' compile with: fbc matx_lrg.bas
#cmdline "-exx"
type Matrix
dim as double m( any , any )
declare constructor ( )
declare constructor ( byval x as uinteger , byval y as uinteger )
end type
'
type Vector
dim as double v( any )
declare constructor ( )
declare constructor ( byval x as uinteger )
end type
'
'
' ---------------------------- operators -------------------------------
'
declare operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator * ( byref a as Matrix , byref b as Vector ) as Vector
declare operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator - ( byref a as Vector , byref b as Vector ) as Vector
declare operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator + ( byref a as Vector , byref b as Vector ) as Vector
declare operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator / ( byref a as Vector , byref b as Vector ) as Vector
' The rest from luxan, sciwiseg@gmail.com
'
' Probably need to make this content
' available under GNU Free Documentation License 1.2
'
'
' ............................. procedures .............................
'
declare sub gen_z(z() as Matrix, idx as Integer)
declare sub gen_v(u() as Vector, idx as Integer)
declare sub prtv_v(u() as Vector, idx as Integer)
declare sub prt_z(z() as Matrix, idx as Integer)
declare sub prt_m(z as Matrix)
declare sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
declare sub prt_v(z as Vector)
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
' Matrix of matrices
'
dim as Matrix z(1 to lms)
'
' Vector of vectors
'
dim as Vector u(0 to lms)
'
' Dimension matrices and vectors .
'
for i=0 to lms-1
nx=m_seq(i)
ny=m_seq(i+1)
z(i+1) = Matrix(ny , nx)
u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
next i
'
' ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
print " Input vector, a matrix in another version ? "
print
prtv_v(u(), 0)
'
print " matrix of matrices values "
print
'
for i=1 to lms
gen_z(z() , i)
prt_z(z() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(i) = z(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms
print " z(";i;") "
prt_z(z() , i)
u(i) = z(i)*u(i-1)
print " u(";i;") "
prtv_v(u(), i)
next i
'
' end command acts as global destructor
' of allocated memory ?
'
' With -exx option, no error messages, with or without end command.
'
end
'
' ======================================================================
'
' Matrix math
'
' 1. dim as Matrix a
' 2. redim a.m( x1 to x2 , y1 to y2 )
'
' 3. dim as Matrix z(z1 to z2) ?
' 4. redim z.m(w1 to w2) ?
'
' Matrix(x , y) ~= redim (0 to x-1, 0 to y-1) ?
' These declarations from :
' http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
' That content is available under
' Content is available under GNU Free Documentation License 1.2 unless otherwise noted.
'
' Using Geany, Build, Set Build Commands
'
' Compile fbc -w all "%f"
'
' Execute "./%e"
'
' Preliminary results checked using Maxima CAS
'
' also at, especially for vector by matrix calculations.
' https://keisan.casio.com/exec/system/15052033860538
'
' https://matrix.reshish.com/multCalculation.php
'
' https://elsenaju.eu/Calculator/matrix-vector-product.htm
'
'
'type Matrix
' dim as double m( any , any )
' declare constructor ( )
' declare constructor ( byval x as uinteger , byval y as uinteger )
'end type
constructor Matrix ( )
end constructor
constructor Matrix ( byval x as uinteger , byval y as uinteger )
redim this.m( x - 1 , y - 1 )
end constructor
'type Vector
'dim as double v( any )
' declare constructor ( )
' declare constructor ( byval x as uinteger )
'end type
constructor Vector ( )
end constructor
constructor Vector ( byval x as uinteger )
redim this.v( x - 1 )
end constructor
'
' ====================================================================
'
Public operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
' Only applicable to square matrices or arrays.
' This routine from :
' http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
'
' Dodicat suggests only using: if ubound( a.m , 2 ) = ubound( b.m , 1 )
'
'
dim as Matrix ret
dim as uinteger i, j, k
'if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
if ubound( a.m , 2 ) = ubound( b.m , 1 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
for k = 0 to ubound( b.m , 1 )
ret.m( i , j ) += a.m( i , k ) * b.m( k , j )
next k
next j
next i
end if
return ret
end operator
'
'
Public operator * ( byref a as Matrix , byref b as Vector ) as Vector
'
' Vector multiplied by a matrix, maybe correct .
'
'
dim as Vector ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.m , 1 ) )
for i = 0 to ubound( a.m , 1 )
for k = 0 to ubound( b.v , 1 )
ret.v( i ) += a.m( i , k ) * b.v( k )
next k
next i
end if
return ret
end operator
'
Public operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
ret.m( i , j ) = a.m( i , j ) - b.m( i , j )
next j
next i
end if
return ret
end operator
'
Public operator - ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
ret.v( i ) = a.v( i ) - b.v( i )
next i
end if
return ret
end operator
'
Public operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
ret.m( i , j ) = a.m( i , j ) + b.m( i , j )
next j
next i
end if
return ret
end operator
'
Public operator + ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
ret.v( i ) = a.v( i ) + b.v( i )
next i
end if
return ret
end operator
'
Public operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
dim as double x, y, z
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
x = a.m( i , j )
y = b.m( i , j )
y = sgn(x)*10^32 ' assume y = 0
if y <> 0 then z = x/y
ret.m( i , j ) = z
next j
next i
end if
return ret
end operator
'
Public operator / ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
dim as double x, y, z
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
x = a.v( i )
y = b.v( i )
y = sgn(x)*10^32 ' assume y = 0
if y <> 0 then z = x/y
ret.v( i ) = z
next i
end if
return ret
end operator
'
' ......................................................................
'
Public sub prt_z(z() as Matrix, idx as Integer)
'
' Matrix of Matrix, index and print .
'
dim as integer i,j
'
'print
for i=0 to ubound(z(idx).m,1)
for j=0 to ubound(z(idx).m,2)
print Using "###.####";z(idx).m(i,j);" ";
next j
print
next i
print
'
end sub
'
' ......................................................................
'
Public sub prt_m(z as Matrix)
'
' Print elements from rectangular matrix .
'
dim as integer i,j
'
print
print ubound(z.m,1);" ";ubound(z.m,2)
print
for i=0 to ubound(z.m,1)
for j=0 to ubound(z.m,2)
print Using "###.####";z.m(i,j);" ";
next j
print
next i
print
'
end sub
'
' ......................................................................
'
Public sub prt_v(z as Vector)
'
' Print elements from vector .
'
dim as integer i
'
'print
for i=0 to ubound(z.v,1)
print Using "###.####";z.v(i);" ";
next i
print
print
'
end sub
'
' ......................................................................
'
Public sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
'
' Multiply vector by matrix .
'
dim as integer i,j,ub1, ub2
dim as single x,y,s
ub1=ubound(M1.m,1)
ub2=ubound(M1.m,2)
'
redim V3.v(0 to ub1)
'
for j=0 to ub1
s=0
for i=0 to ub2
x=V1.v(i)
y=M1.m(j,i)
s=s+x*y
next i
V3.v(j)=s
next j
'
end sub
'
' ......................................................................
'
Public sub gen_z(z() as Matrix, idx as Integer)
'
' Matrix of Matrix, index and generate values .
'
dim as integer i,j
'
Randomize
for j=0 to ubound(z(idx).m,2)
for i=0 to ubound(z(idx).m,1)
z(idx).m(i,j) = -1 + 2*rnd
next i
next j
'
end sub
'
' ......................................................................
'
Public sub gen_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and generate values .
'
dim as integer i,j
Randomize
'
for i=0 to ubound(u(idx).v,1)
u(idx).v(i) = -1 + 2*rnd
next i
'
end sub
'
' ......................................................................
'
Public sub prtv_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and print values .
'
dim as integer i,j
'
for i=0 to ubound(u(idx).v,1)
print Using "###.####";u(idx).v(i)
next i
print
'
end sub
'
' ......................................................................
'
Code: Select all
type Matrix
dim as double m( any , any )
declare constructor ( )
declare constructor ( array( any, any ) as double )
end type
constructor Matrix ( )
end constructor
constructor Matrix ( array( any, any ) as double )
if ubound(array, 2) > = 0 and lbound(array, 1) = 0 and lbound(array, 2) = 0 Then '' passed array non-empty with all indexes 0 based
redim m(ubound(array, 1), ubound(array, 2))
fb_memcopy(m(0, 0), array(0, 0), (ubound(array, 1) + 1) * (ubound(array, 2) + 1) * sizeof(double)) '' shallow copy because DOUBLE type
end if
end constructor
'------------------------------------------------------------------------------
dim X(0 to 5,0 to 2) as double => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
dim M as Matrix = Matrix(X())
'------------------------------------------------------------------------------
for I as integer = 0 to ubound(M.m, 1)
for J as integer = 0 to ubound(M.m, 2)
print M.m(I, J),
next J
print
next I
sleep
Code: Select all
#cmdline "-exx"
Type Matrix
As Double e(any,any)
End Type
sub show(m as matrix)
Dim As String ans,comma
for r as long=0 to ubound(m.e,1)
for c as long=0 to ubound(m.e,2)
if c=0 then print "[";
If c=Ubound(m.e,2) Then comma="" Else comma=","
print str(m.e(r,c));comma;
if c=ubound(m.e,2) then print "]";
next c
print
next r
end sub
#macro setmatrix(m,r,c,d...)
#macro spl()
s+=","
For n As Long=0 To Len(s)-1
t+=Chr(s[n])
If s[n]=44 Then
Redim Preserve db(Ubound(db)+1)
db(Ubound(db))=Val(t)
t=""
End If
Next n
#endmacro
Scope
Dim As String t
Redim As Double db()
Dim As Long count
Var s=#d
Redim m.e(0 To r,0 To c)
spl()
For r1 As Long=0 To r
For c1 As Long=0 To c
m.e(r1,c1)= db(count)
count+=1
Next
Next
End Scope
#endmacro
dim as matrix m
dim as long r=5
dim as long c=2
setmatrix(m,r,c, 0,0,1,_
0,1,1,_
1,0,0,_
1,1,0,_
1,0,1,_
1,1,1)
show(m)
print
setmatrix(m,2,2, 4,6.6,7,_
1,-7,4,_
.05,23,2)
show(m)
print
setmatrix(m,3,0, 8,_
7,_
-6,_
5.5)
show(m)
print
setmatrix(m,0,3, 8,7,-6,5.5)
show(m)
sleep
Code: Select all
' easy_matx.bas
' On my computer
' cd Downloads/fb/matrices_x/dodicat/merge_1/lib
'
' ' compile with: fbc -lib easy_matx.bas
' Matrix math
'
' 1. dim as Matrix a
' 2. redim a.m( x1 to x2 , y1 to y2 )
'
' 3. dim as Matrix z(z1 to z2) ?
' 4. redim z.m(w1 to w2) ?
'
' Matrix(x , y) ~= redim (0 to x-1, 0 to y-1) ?
' These declarations from :
' http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
' That content is available under
' Content is available under GNU Free Documentation License 1.2 unless otherwise noted.
'
' Using Geany, Build, Set Build Commands
'
' Compile fbc -w all "%f"
'
' Execute "./%e"
'
' Preliminary results checked using Maxima CAS
'
' also at, especially for vector by matrix calculations.
' https://keisan.casio.com/exec/system/15052033860538
'
' https://matrix.reshish.com/multCalculation.php
'
' https://elsenaju.eu/Calculator/matrix-vector-product.htm
'
'
type Matrix
dim as double m( any , any )
declare constructor ( )
declare constructor ( byval x as uinteger , byval y as uinteger )
end type
constructor Matrix ( )
end constructor
constructor Matrix ( byval x as uinteger , byval y as uinteger )
redim this.m( x - 1 , y - 1 )
end constructor
type Vector
dim as double v( any )
declare constructor ( )
declare constructor ( byval x as uinteger )
end type
constructor Vector ( )
end constructor
constructor Vector ( byval x as uinteger )
redim this.v( x - 1 )
end constructor
'
' ====================================================================
'
Public operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
' Only applicable to square matrices or arrays.
' This routine from :
' http://www.rosettacode.org/wiki/Matrix_multiplication#FreeBASIC
'
' Dodicat suggests only using: if ubound( a.m , 2 ) = ubound( b.m , 1 )
'
'
dim as Matrix ret
dim as uinteger i, j, k
'if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
if ubound( a.m , 2 ) = ubound( b.m , 1 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
for k = 0 to ubound( b.m , 1 )
ret.m( i , j ) += a.m( i , k ) * b.m( k , j )
next k
next j
next i
end if
return ret
end operator
'
'
Public operator * ( byref a as Matrix , byref b as Vector ) as Vector
'
' Vector multiplied by a matrix, maybe correct .
'
'
dim as Vector ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.m , 1 ) )
for i = 0 to ubound( a.m , 1 )
for k = 0 to ubound( b.v , 1 )
ret.v( i ) += a.m( i , k ) * b.v( k )
next k
next i
end if
return ret
end operator
'
Public operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
ret.m( i , j ) = a.m( i , j ) - b.m( i , j )
next j
next i
end if
return ret
end operator
'
Public operator - ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
ret.v( i ) = a.v( i ) - b.v( i )
next i
end if
return ret
end operator
'
Public operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
ret.m( i , j ) = a.m( i , j ) + b.m( i , j )
next j
next i
end if
return ret
end operator
'
Public operator + ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
ret.v( i ) = a.v( i ) + b.v( i )
next i
end if
return ret
end operator
'
Public operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
dim as Matrix ret
dim as uinteger i, j, k
dim as double x, y, z
if ubound( a.m , 2 ) = ubound( b.m , 1 ) and ubound( a.m , 1 ) = ubound( b.m , 2 ) then
redim ret.m( ubound( a.m , 1 ) , ubound( b.m , 2 ) )
for i = 0 to ubound( a.m , 1 )
for j = 0 to ubound( b.m , 2 )
x = a.m( i , j )
y = b.m( i , j )
y = sgn(x)*10^32 ' assume y = 0
if y <> 0 then z = x/y
ret.m( i , j ) = z
next j
next i
end if
return ret
end operator
'
Public operator / ( byref a as Vector , byref b as Vector ) as Vector
dim as Vector ret
dim as uinteger i
dim as double x, y, z
if ubound( a.v , 1 ) = ubound( b.v , 1 ) then
redim ret.v( ubound( a.v , 1 ) )
for i = 0 to ubound( a.v , 1 )
x = a.v( i )
y = b.v( i )
y = sgn(x)*10^32 ' assume y = 0
if y <> 0 then z = x/y
ret.v( i ) = z
next i
end if
return ret
end operator
'
' ......................................................................
'
Public sub prt_z(z() as Matrix, idx as Integer)
'
' Matrix of Matrix, index and print .
'
dim as integer i,j
'
'print
for i=0 to ubound(z(idx).m,1)
for j=0 to ubound(z(idx).m,2)
print Using "###.####";z(idx).m(i,j);" ";
next j
print
next i
print
'
end sub
'
' ......................................................................
'
Public sub prt_m(z as Matrix)
'
' Print elements from rectangular matrix .
'
dim as integer i,j
'
print
print ubound(z.m,1);" ";ubound(z.m,2)
print
for i=0 to ubound(z.m,1)
for j=0 to ubound(z.m,2)
print Using "###.####";z.m(i,j);" ";
next j
print
next i
print
'
end sub
'
' ......................................................................
'
Public sub prt_v(z as Vector)
'
' Print elements from vector .
'
dim as integer i
'
'print
for i=0 to ubound(z.v,1)
print Using "###.####";z.v(i);" ";
next i
print
print
'
end sub
'
' ......................................................................
'
Public sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
'
' Multiply vector by matrix .
'
dim as integer i,j,ub1, ub2
dim as single x,y,s
ub1=ubound(M1.m,1)
ub2=ubound(M1.m,2)
'
redim V3.v(0 to ub1)
'
for j=0 to ub1
s=0
for i=0 to ub2
x=V1.v(i)
y=M1.m(j,i)
s=s+x*y
next i
V3.v(j)=s
next j
'
end sub
'
' ......................................................................
'
Public sub gen_z(z() as Matrix, idx as Integer)
'
' Matrix of Matrix, index and generate values .
'
dim as integer i,j
'
Randomize
for j=0 to ubound(z(idx).m,2)
for i=0 to ubound(z(idx).m,1)
z(idx).m(i,j) = -1 + 2*rnd
next i
next j
'
end sub
'
' ......................................................................
'
Public sub gen_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and generate values .
'
dim as integer i,j
Randomize
'
for i=0 to ubound(u(idx).v,1)
u(idx).v(i) = -1 + 2*rnd
next i
'
end sub
'
' ......................................................................
'
Public sub prtv_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and print values .
'
dim as integer i,j
'
for i=0 to ubound(u(idx).v,1)
print Using "###.####";u(idx).v(i)
next i
print
'
end sub
'
' ......................................................................
'
Public sub Sigmoid_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and Sigmoid function of values .
'
dim as integer i,j
dim as single y
'
for i=0 to ubound(u(idx).v,1)
y=u(idx).v(i)
y=1/(1+exp(-y))
u(idx).v(i) = y
next i
print
'
end sub
'
' ......................................................................
'
Public sub dSigmoid_v(u() as Vector, idx as Integer)
'
' Vector of Vector, index and Derivative of Sigmoid function of values .
'
dim as integer i,j
dim as single y
'
for i=0 to ubound(u(idx).v,1)
y=u(idx).v(i)
'y=1/(1+exp(-y))
y=-exp(-y)/(1-exp(-y))^2
u(idx).v(i) = y
next i
print
'
end sub
'
' ......................................................................
'
Code: Select all
' easy_matx.bi
#inclib "easy_matx"
' For some reason, may need to duplicate types, but not constructors,
' in this *.bi
type Matrix
dim as double m( any , any )
declare constructor ( )
declare constructor ( byval x as uinteger , byval y as uinteger )
end type
'
type Vector
dim as double v( any )
declare constructor ( )
declare constructor ( byval x as uinteger )
end type
'
'
' ---------------------------- operators -------------------------------
'
declare operator * ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator * ( byref a as Matrix , byref b as Vector ) as Vector
declare operator - ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator - ( byref a as Vector , byref b as Vector ) as Vector
declare operator + ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator + ( byref a as Vector , byref b as Vector ) as Vector
declare operator / ( byref a as Matrix , byref b as Matrix ) as Matrix
declare operator / ( byref a as Vector , byref b as Vector ) as Vector
' The rest from luxan, sciwiseg@gmail.com
'
' Probably need to make this content
' available under GNU Free Documentation License 1.2
'
'
' ............................. procedures .............................
'
declare sub gen_z(z() as Matrix, idx as Integer)
declare sub gen_v(u() as Vector, idx as Integer)
declare sub prtv_v(u() as Vector, idx as Integer)
declare sub prt_z(z() as Matrix, idx as Integer)
declare sub prt_m(z as Matrix)
declare sub Vect_x_Matrix(M1 as Matrix, V1 as Vector, V3 as Vector)
declare sub prt_v(z as Vector)
'
declare sub Sigmoid_v(u() as Vector, idx as Integer)
declare sub dSigmoid_v(u() as Vector, idx as Integer)
Code: Select all
'
' matx_smv.bas
'
' Successive matrix vector multiplications .
'
'' compile with: fbc matx_test.bas
#include once "easy_matx.bi"
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
' Matrix of matrices
'
dim as Matrix W(1 to lms)
dim as Matrix X = Matrix(6,3)
dim as integer j,ux,uy
'dim as Matrix X = Matrix((0, 0, 1),(0, 1, 1),(1,0,0),(1,1,0),(1,0,1),(1,1,1))
'end
dim Xa(0 to 5,0 to 2) as integer => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
X.m={{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
ux=ubound(Xa,1)
uy=ubound(Xa,2)
for j=0 to uy
for i=0 to ux
X.m(i,j) = Xa(i,j)
next i
next j
'
prt_m(X)
print
print " ux = ";ux
dim as Vector y = Vector(ux+1)
'dim ya(0 to ux+1) as integer
dim ya(0 to 5) as integer => {(0),(1),(0),(1),(1),(0)}
'redim ya(0 to ux) as integer => {0,1,0,1,1,0}
'ya(0 to ux) => {0,1,0,1,1,0}
for i=0 to ux+1
y.v(i) = ya(i)
next i
prt_v(y)
end
'
' Vector of vectors
'
dim as Vector u(0 to lms) ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
' Dimension matrices and vectors .
'
for i=0 to lms-1
nx=m_seq(i)
ny=m_seq(i+1)
W(i+1) = Matrix(ny , nx)
u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
up(i) = Vector(nx) ' subtract bias vector, apply derivative of threshold function; return ?
ub(i+1) = Vector(ny) ' bias vector .
gen_v(ub(), i+1) ' bias vector[s], random values [-1,1] .
next i
'
' ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print " matrix of matrices values "
print
'
for i=1 to lms
gen_z(W() , i)
'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms
print " W(";i;") "
prt_z(W() , i)
u(i) = W(i)*u(i-1)
u(i) = u(i) - ub(i)
up(i) = u(i)
Sigmoid_v(u(), i)
dSigmoid_v(up(), i)
print " u(";i;") "
prtv_v(u(), i)
print " u(";i;")' "
prtv_v(up(), i)
next i
' end command acts as global destructor
' of allocated memory ?
end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7
Code: Select all
'
' bp_smv.bas
'
' Successive matrix vector multiplications .
' back propagation preliminaries.
'
'' compile with: fbc bp_smv.bas
#cmdline "-exx"
#include once "easy_matx.bi"
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
' Matrix of matrices
'
dim as Matrix W(1 to lms)
dim as Matrix X = Matrix(6,3)
dim as integer j,ux,uy
'dim as Matrix X = Matrix((0, 0, 1),(0, 1, 1),(1,0,0),(1,1,0),(1,0,1),(1,1,1))
'end
dim Xa(0 to 5,0 to 2) as integer => {{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
'X.m={{0, 0, 1},{0, 1, 1},{1,0,0},{1,1,0},{1,0,1},{1,1,1}}
ux=ubound(Xa,1)
uy=ubound(Xa,2)
for j=0 to uy
for i=0 to ux
X.m(i,j) = Xa(i,j)
next i
next j
'
prt_m(X)
print
print " ux = ";ux
dim as Vector y = Vector(ux+1)
'dim ya(0 to ux+1) as integer
dim ya(0 to 5) as integer => {(0),(1),(0),(1),(1),(0)}
'redim ya(0 to ux) as integer => {0,1,0,1,1,0}
'ya(0 to ux) => {0,1,0,1,1,0}
for i=0 to ux+1
y.v(i) = ya(i)
next i
prt_v(y)
end
'
' Vector of vectors
'
dim as Vector u(0 to lms) ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
' Dimension matrices and vectors .
'
for i=0 to lms-1
nx=m_seq(i)
ny=m_seq(i+1)
W(i+1) = Matrix(ny , nx)
u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
up(i) = Vector(nx) ' subtract bias vector, apply derivative of threshold function; return ?
ub(i+1) = Vector(ny) ' bias vector .
gen_v(ub(), i+1) ' bias vector[s], random values [-1,1] .
next i
'
' ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print " matrix of matrices values "
print
'
for i=1 to lms
gen_z(W() , i)
'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms
print " W(";i;") "
prt_z(W() , i)
u(i) = W(i)*u(i-1)
u(i) = u(i) - ub(i)
up(i) = u(i)
Sigmoid_v(u(), i)
dSigmoid_v(up(), i)
print " u(";i;") "
prtv_v(u(), i)
print " u(";i;")' "
prtv_v(up(), i)
next i
' end command acts as global destructor
' of allocated memory ?
end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7
Code: Select all
'
' matx_smv.bas
'
' Successive matrix vector multiplications .
' And evaluation of sigmoid, derivative of sigmoid.
'
'' compile with: fbc matx_smv.bas
#cmdline "-exx"
#include once "easy_matx.bi"
'
' ----------------------------------------------------------------------
'
dim as integer m_seq(0 to 3)={8,6,3,5}
dim as integer lms,nx,ny,i
lms=ubound(m_seq,1)
print(lms)
'
' Matrix of matrices
'
dim as Matrix W(1 to lms)
'
' Vector of vectors
'
dim as Vector u(0 to lms) ' input and layer vectors, store activation function values.
dim as Vector up(0 to lms) ' layer vectors, store activation function derivative values.
dim as Vector ub(0 to lms) ' bias vectors, associated with each layer .
'
' Dimension matrices and vectors .
'
for i=0 to lms-1
nx=m_seq(i)
ny=m_seq(i+1)
W(i+1) = Matrix(ny , nx)
u(i) = Vector(nx) ' subtract bias vector, apply threshold function; return ?
up(i) = Vector(nx) ' subtract bias vector, apply derivative of threshold function; return ?
ub(i+1) = Vector(ny) ' bias vector .
gen_v(ub(), i+1) ' bias vector[s], random values [-1,1] .
next i
'
' ............... Assign values to input vector & matrices ............
'
gen_v(u(), 0)
print
'print " Input vector, a matrix in another version ? "
print
'prtv_v(u(), 0)
'
'print " matrix of matrices values "
print
'
for i=1 to lms
gen_z(W() , i)
'prt_z(W() , i)
next i
print
'
print " sucessive vectors , matrices & vector x matrix calculations"
print " u(0) is the input vector "
print " u(i) = W(i) * u(i - 1) "
print
'
print " u(0) "
prtv_v(u(), 0)
for i=1 to lms
print " W(";i;") "
prt_z(W() , i)
u(i) = W(i)*u(i-1)
u(i) = u(i) - ub(i)
up(i) = u(i)
Sigmoid_v(u(), i)
dSigmoid_v(up(), i)
print " u(";i;") "
prtv_v(u(), i)
print " u(";i;")' "
prtv_v(up(), i)
next i
' end command acts as global destructor
' of allocated memory ?
end
'
' ======================================================================
'
' Input data set includes expected output value[s]
' These expected values are used in calculations, including those for backpropagation.
'
'https://towardsdatascience.com/how-does-back-propagation-in-artificial-neural-networks-'work-c7cad873ea7