* {*********************************************************************

File: var/matrix/setmat.cm

  In this file some effects on the speed are considered and solutions for
speed-up are suggested.

Author: Igor Gresovnik, May 1998

************************************************************************}

setfile{outfile speedmat.ct}


* {
*****************  SETTING MATRIX COMPONENTS  ******************
  You can easily set matrix components by using the file interpreter command
"setmatrix", but this can be slow because of the interpretation of the command
file. This is especially disturbing if a lot of components should be set.
  If all matrix components should be set to the values of a specific
expression, you can use the "setmatrixcomponents" function. This function
iterates over all components of a matrix or of a group of matrices and assign
them values specified by a mathematical expression. In this expression the user
can use functions which return the numbers of a component to wich the value of
the expression is currently assigned, and function which returns indeces of a
matrix which is currently updated, on the variable,s value stack.
*******************************************************************************
}


write{\n"******  SPEED TEST: setmatrixcomponents vs. setmatrix  ******"\n\n}
fwrite{\n"******  SPEED TEST: setmatrixcomponents vs. setmatrix  ******"\n\n}


write{"Preparation of a matrix variable:"\n\n}

newmatrix{m1 [2 3] }
setmatrix{m1 [1 1] 20 20}
setmatrix{m1 [1 2] 20 20}
setmatrix{m1 [1 3] 20 20}
setmatrix{m1 [2 1] 20 20}
setmatrix{m1 [2 2] 20 20}
setmatrix{m1 [2 3] 20 20}

write{"Setting components:"\n\n}

write{"Before setmatrixcomponents:"\n}

setmatrixcomponents{m1[] 100*varindex[1]+10*varindex[2]+
                       0.1*varcomponent[1]+0.01*varcomponent[2]}

write{"After setmatrixcomponents."\n\n\n}

fwrite{\n\n"Matrix which was set by function setmatrixcomponents:"\n}
fprintmatrixvar{m1}

write{"Before setmatrix:"\n}

break{}


${auxexp:1000*i+10*j+0.1*k+0.01*l}
={i:1}
while{(i<=2)
[
  ={j:1}
  while{(j<=3)
  [
    ={k:1}
    while{(k<=20)
    [
      ={l:1}
      while{(l<=20)
      [
        setmatrix { m1[$i $j]
          { $k $l : auxexp }
        }
      
        *
        {
        write{"i = "$i",j = "$j",k = "$k",l = "$l\n}
        }
        
        ={l:l+1}
        
      ]}
      ={k:k+1}
    ]}
    ={j:j+1}
  ]}
  ={i:i+1}
]}



write{"After setmatrix:"\n\n}
fwrite{\n\n"Matrix which was set by function setmatrixcomponents:"\n}
fprintmatrixvar{m1}