Matlib (for lineair algebra guy's)

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Matlib (for lineair algebra guy's)

Post by Henko »

option base 1
dim t(3,3),t1(4,3),t2(3,3),s(3),s1(3)
maxx=screen_width()
maxy=screen_height()
randomize
graphics
fill color 0.8,0.8,0.8
cls()
rem fill rect 0,0 to maxx,maxy
draw color 0,0,0
for i=1 to 3
  for j=1 to 3
    read t(i,j)
  next j
next i
vec_rnd(3,s,.1,.7)
rem vec_in (3,s,600,20)
mat_in (4,3,t1,200,500)
vec_norm(3,s)
vec_uit("",3,s,400,20,7,4)
data .3,.1,0,.7,.6,.2,0,.3,.8
rem data -.7,.1,0,.7,-.4,.2,0,.3,-.2
mat_copy (3,3,t,t1)
for i=1 to 20
  mat_mul (3,3,t,t1,t2)
  mat_copy (3,3,t2,t1)
next i  
mat_uit ("",3,3,t1,10,10,8,5)
mat_vec (3,3,t1,s,s1)
vec_uit ("",3,s1,200,600,8,5)
end

rem vec_in (n,v,x,y)
rem vec_uit (n$,n,v,x,y,l,d)
rem vec_nul (n,v)
rem vec_een (n,v)
rem vec_rnd (n,v,min,max)
rem vec_copy (n,van,naar)
rem vec_scal (n,v,s)
rem vec_norm (n,v)
rem vec_plus (n,v1,v2)
rem vec_min (n,v1,v2)
rem inprodukt (n,v1,v2)
rem mat_in (n,m,mat,x,y)
rem mat_uit (n$,n,m,mat,x,y,l,d)
rem mat_nul (n,m,mat)
rem mat_een (n,mat)
rem mat_rnd (n,m,mat,min,max)
rem mat_scal (n,m,mat,s)
rem mat_copy (n,m,van,naar)
rem mat_trans (n,m,mat,matt)
rem mat_plus (n,m,mata,matb)
rem mat_min (n,m,mata,matb)
rem mat_mul (n,m,mata,matb,matc)
rem mat_inv (n,mata,ainv)
rem mat_vec (n,m,mat,vin,vuit)
rem def ls (n,m,x,y,c)
rem def poly (n,coef,x)
rem def pi
rem def abs (x)
rem def mod (a,m)
rem def rest (a,b)
rem def exp (x)
rem def ln(x)
rem def log(x)
rem cls()

def vec_in (n,v(),x,y)
dim b$(20)
for i=1 to n
b$(i)="a" & i ! field b$(i) text b$(i) at x,y+30*(i-1) size 80,25
next i
som=0
loop:
for i=1 to n
if field_changed(b$(i)) then
  v(i)=field_text$(b$(i)) ! field b$(i) delete
  draw text n2a$(v(i),6,2) at x,y+30*(i-1)
  som=som+1
end if
next i
if som<n then goto loop
end def

def vec_uit (n$,n,v(),x,y,l,d)
ys=y-30
if n$<>"" then
  draw text n$ at x,y
  ys=ys+30
end if
for i=1 to n
  a$=n2a$(i,3,0) & " " & n2a$(v(i),l,d)
  draw text a$ at x-12,ys+25*i
next i
end def

def vec_nul (n,v())
for i=1 to n ! v(i)=0 ! next i
end def

def vec_een (n,v())
for i=1 to n ! v(i)=1 ! next i
end def

def vec_rnd (n,v(),min,max)
for i=1 to n ! v(i)=min+(max-min)*rnd(1) ! next i
end def

def vec_copy (n,van(),naar())
for i=1 to n ! naar(i)=van(i) ! next i
end def

def vec_scal (n,v(),s)
for i=1 to n ! v(i)=s*v(i) ! next i
end def

def vec_norm (n,v())
som=0
for i=1 to n ! som=som+v(i) ! next i
if som=0 then fac=1 else fac=1/som
vec_scal (3,v,fac)
end def

def vec_plus (n,v1(),v2())
for i=1 to n ! v1(i)=v1(i)+v2(i) ! next i
end def

def vec_min (n,v1(),v2())
for i=1 to n ! v1(i)=v1(i)-v2(i) ! next i
end def

def inprodukt (n,v1(),v2())
som=0
for i=1 to n ! som=som+v1(i)*v2(i) ! next i
inprodukt=som
end def

def mat_in (n,m,mat(,),x,y)
dim b$(100)
b$(1)=0
for i=1 to n
  for j=1 to m
    k=m*(i-1)+j ! b$(k)="     a" & i & j ! tt$=b$(k)
    field tt$ text tt$ at x+100*(j-1),y+30*(i-1) size 80,25
  next j
next i
som=0
loop1:
for i=1 to n
  for j=1 to m
    k=m*(i-1)+j
    if field_changed(b$(k)) then
      mat(i,j)=field_text$(b$(k))
      field b$(k) delete
      draw text n2a$(mat(i,j),6,2) at x+100*(j-1),y+30*(i-1)
      som=som+1
    end if
  next j
next i
if som<n*m then goto loop1
end def

def mat_nul (n,m,mat(,))
for i=1 to n
  for j=1 to m ! mat(i,j)=0 ! next j
next i
end def

def mat_een (n,mat(,))
for i=1 to n
  for j=1 to n ! if i=j then mat(i,j)=1 else mat(i,j)=0 ! next j
next i
end def

def mat_rnd (n,m,mat(,),min,max)
for i=1 to n
  for j=1 to m ! mat(i,j)=min+(max-min)*rnd(1) ! next j
next i
end def

def mat_scal (n,m,mat(,),s)
for i=1 to n
  for j=1 to m ! mat(i,j)=s*mat(i,j) ! next j
next i
end def

def mat_copy (n,m,van(,),naar(,))
for i=1 to n
  for j=1 to m ! naar(i,j)=van(i,j) ! next j
next i
end def

def mat_trans (n,m,mat(,),matt(,))
for i=1 to n
  for j=1 to m ! matt(j,i)=mat(i,j) ! next j
next i
end def

def mat_plus (n,m,mata(,),matb(,))
for i=1 to n
  for j=1 to m ! mata( i,j)=mata(i,j)+matb(i,j) ! next j
next i
end def

def mat_min (n,m,mata(,),matb(,))
for i=1 to n
  for j=1 to m ! mata( i,j)=mata(i,j)-matb(i,j) ! next j
next i
end def

def mat_mul (n,m,mata(,),matb(,),matc(,))
for i=1 to n
  for j=1 to n
    tot=0
    for k=1 to m ! tot=tot+mata(i,k)*matb(k,j) ! next k
    matc(i,j)=tot
    next j
  next i
end def

def mat_inv (nvar,a(,),ainv(,))
dim w(20,40)                      
for i=1 to nvar                 
  for j=1 to nvar ! w(i,j)=a(i,j) ! w(i,j+nvar)=0  ! next j
  w(i,i+nvar)=1
  next i
for piv=1 to nvar
  fac=w(piv,piv)
  for j=piv to piv+nvar ! w(piv,j)=w(piv,j)/fac ! next j
  for i=1 to nvar
    IF i<>piv then
      fac=w(i,piv)
      for j=piv to piv+nvar ! w(i,j)=w(i,j)-fac*w(piv,j) ! next j
      endif
    next i
  next piv
for i=1 to nvar
  for j=1 to nvar ! ainv(i,j)=w(i,j+nvar) ! next j
  next i
end def

def mat_uit (n$,n,m,mat(,),x,y,l,d)
ys=y-30
if n$<>"" then
  draw text n$ at x,y
  ys=ys+30
end if
for i=1 to n
  for j=1 to m
    a$=n2a$(mat(i,j),l,d) ! draw text a$ at x+12*l*(j-1),ys+25*i
  next j
next i
end def

def mat_vec (n,m,mat(,),vin(),vuit())
for i=1 to n
  tot=0
  for j=1 to m ! tot=tot+mat( i,j)*vin(j) ! next j
  vuit(i)=tot
next i
end def

def ls (n,m,x(),y(),c())
dim a1(20,20),a2(20,20),a3(20,20),rl(20)
a1(1,1)=0 ! a2(1,1)=0 ! a3(1,1)=0 ! rl(1)=0
m=m+1
for i=1 to n
  a1(i,1)=1
  for j=2 to m ! a1(i,j)=a1(i,j-1)*x(i) ! next j
next i
mat_trans (n,m,a1,a2)
mat_mul (m,n,a2,a1,a3)
for i=1 to m
  tot=0
  for j=1 to n ! tot=tot+a2( i,j)*y(j) ! next j
  rl(i)=tot
next i
mat_inv (m,a3,a1)
for i=1 to m
  tot=0
  for j=1 to n ! tot=tot+a1(i,j)*rl(j) ! next j
  c(i)=tot
next i
end def

def poly (n,coef(),x)
res=coef(n+1)
for i=n to 1 step-1 ! res=res*x+coef(i) ! next i
poly=res
end def

def pi
pi=3.14159
end def

def abs (x)
if x<0 then abs=-x else abs=x
end def

def mod(a,m)
d=a/m
mod=m*(d-int(d))
end def

def rest (a,b)
q=a/b
rest=q-int(q)
end def

def exp (x)
e=1
for i=11 to 1 step-1 ! e=e*x/i+1 ! next i
exp=e
end def

def ln(a)
if a<=0 then
  ln=-99999
  return
end if
e=2.718282 ! h=a ! x=0
loopln0:
h=h/e
if h>1 then 
  x=x+1
  goto loopln0
end if
loopln:
dx=a/(e^x)-1 ! x=x+dx
if abs(dx)>0.00001 then goto loopln
ln=x
end def

def log(a)
if a<=0 then
  log=-99999
  return
end if
h=a ! x=0
looplog0:
h=h/10
if h>1 then 
  x=x+1
  goto looplog0
end if
looplog:
dx=.4343*(a/(10^x)-1) ! x=x+dx
if abs(dx)>0.00001 then goto looplog
log=x
end def

def n2a$(num,lang,dec)
b$="               "
fac=10^dec
num$=int(fac*num)/fac
tot=lang-len(num$)
if tot<1 then tot=1
a$=substr$(b$,1,tot) & num$
n2a$=a$
end def

def pre_pad$(w,a$)
sp$="               "
tot=w-len(a$)
pre_pad$=substr$(sp$,1,tot) & a$
end def

def test (in$,in)
fill rect 30,900 to 400,940
a$="test " & in$ & in
draw text a$ at 30,900
button "cont" title "doorgaan ?" at 440,890
loopje:
if button_pressed("cont")=0 then goto loopje
button "cont" delete
fill rect 30,900 to 400,940
end def

def cls()
fill rect 0,0 to screen_width(),screen_height()
end def

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Matlib (for lineair algebra guy's)

Post by Operator »

:shock: WOW :shock:
... it's already done ...
Will surely study the functions...

Post Reply