;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; Includes information derived from the GSL (Gnu Scientific Library)
;;;   header files which are copyrighted and distributed under the GPL
;;;   by their respective authors.
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "htk/htk-config")



#? *** htk_math

;; dummy function that adds HTK C header files in C file
(eval `(de dummy_htk_math () ,htk-cpheader-cmd ()))

#? * (dummy_htk_math)
#? * (initmath)
#? * (zeroshortvec <v>)
#? * (zerointvec <v>)
#? * (zerovector <v>)
#? * (zerodvector <v>)
#? * (copyshortvec <v1> <v2>)
#? * (copyintvec <v1> <v2>)
#? * (copyvector <v1> <v2>)
#? * (copydvector <v1> <v2>)
#? * (readshortvec <src> <v> <binary>)
#? * (readintvec <src> <v> <binary>)
#? * (readvector <src> <v> <binary>)
#? * (writeshortvec <f> <v> <binary>)
#? * (writeintvec <f> <v> <binary>)
#? * (writevector <f> <v> <binary>)
#? * (showshortvec <title> <v> <maxterms>)
#? * (showintvec <title> <v> <maxterms>)
#? * (showvector <title> <v> <maxterms>)
#? * (showdvector <title> <v> <maxterms>)
#? * (zeromatrix <m>)
#? * (zerodmatrix <m>)
#? * (zerotrimat <m>)
#? * (copymatrix <m1> <m2>)
#? * (copydmatrix <m1> <m2>)
#? * (copytrimat <m1> <m2>)
#? * (mat2dmat <m1> <m2>)
#? * (dmat2mat <m1> <m2>)
#? * (mat2tri <m1> <m2>)
#? * (tri2mat <m1> <m2>)
#? * (readmatrix <src> <m> <binary>)
#? * (readtrimat <src> <m> <binary>)
#? * (writematrix <f> <m> <binary>)
#? * (writetrimat <f> <m> <binary>)
#? * (showmatrix <title> <m> <maxcols> <maxrows>)
#? * (showdmatrix <title> <m> <maxcols> <maxrows>)
#? * (showtrimat <title> <m> <maxcols> <maxrows>)
#? * (covinvert <c> <invc>)
#? * (covdet <c>)
#? * (svd <a> <u> <v> <d>)
#? * (invsvd <a> <u> <w> <v> <result>)
#? * (ladd <x> <y>)
#? * (lsub <x> <y>)
#? * (l2f <x>)
#? * (randinit <seed>)
#? * (randomvalue)
#? * (gaussdeviate <mu> <sigma>)

;; ********************************************************


(de InitMath (  )
    #{ InitMath(  );  #} 
    ()
)


(de ZeroShortVec (  v )
    ((-gptr- "short *") v)  
    #{ ZeroShortVec(  $v  );  #} 
    ()
)


(de ZeroIntVec (  v )
    ((-gptr- "int *") v)  
    #{ ZeroIntVec(  $v  );  #} 
    ()
)


(de ZeroVector (  v )
    ((-gptr- "float *") v)  
    #{ ZeroVector(  $v  );  #} 
    ()
)


(de ZeroDVector (  v )
    ((-gptr- "double *") v)  
    #{ ZeroDVector(  $v  );  #} 
    ()
)


(de CopyShortVec (  v1  v2 )
    ((-gptr- "short *") v1) 
    ((-gptr- "short *") v2)  
    #{ CopyShortVec(  $v1   ,$v2  );  #} 
    ()
)


(de CopyIntVec (  v1  v2 )
    ((-gptr- "int *") v1) 
    ((-gptr- "int *") v2)  
    #{ CopyIntVec(  $v1   ,$v2  );  #} 
    ()
)


(de CopyVector (  v1  v2 )
    ((-gptr- "float *") v1) 
    ((-gptr- "float *") v2)  
    #{ CopyVector(  $v1   ,$v2  );  #} 
    ()
)


(de CopyDVector (  v1  v2 )
    ((-gptr- "double *") v1) 
    ((-gptr- "double *") v2)  
    #{ CopyDVector(  $v1   ,$v2  );  #} 
    ()
)


(de ReadShortVec (  src  v  binary )
    ((-gptr- "Source *") src) 
    ((-gptr- "short *") v) 
    ((-int-) binary)  
    (int   #{ ReadShortVec(  $src   ,$v   ,$binary  )  #} )
)


(de ReadIntVec (  src  v  binary )
    ((-gptr- "Source *") src) 
    ((-gptr- "int *") v) 
    ((-int-) binary)  
    (int   #{ ReadIntVec(  $src   ,$v   ,$binary  )  #} )
)


(de ReadVector (  src  v  binary )
    ((-gptr- "Source *") src) 
    ((-gptr- "float *") v) 
    ((-int-) binary)  
    (int   #{ ReadVector(  $src   ,$v   ,$binary  )  #} )
)


(de WriteShortVec (  f  v  binary )
    ((-gptr- "FILE *") f) 
    ((-gptr- "short *") v) 
    ((-int-) binary)  
    #{ WriteShortVec(  $f   ,$v   ,$binary  );  #} 
    ()
)


(de WriteIntVec (  f  v  binary )
    ((-gptr- "FILE *") f) 
    ((-gptr- "int *") v) 
    ((-int-) binary)  
    #{ WriteIntVec(  $f   ,$v   ,$binary  );  #} 
    ()
)


(de WriteVector (  f  v  binary )
    ((-gptr- "FILE *") f) 
    ((-gptr- "float *") v) 
    ((-int-) binary)  
    #{ WriteVector(  $f   ,$v   ,$binary  );  #} 
    ()
)


(de ShowShortVec (  title  v  maxterms )
    ((-gptr- "char *") title) 
    ((-gptr- "short *") v) 
    ((-int-) maxterms)  
    #{ ShowShortVec(  $title   ,$v   ,$maxterms  );  #} 
    ()
)


(de ShowIntVec (  title  v  maxterms )
    ((-gptr- "char *") title) 
    ((-gptr- "int *") v) 
    ((-int-) maxterms)  
    #{ ShowIntVec(  $title   ,$v   ,$maxterms  );  #} 
    ()
)


(de ShowVector (  title  v  maxterms )
    ((-gptr- "char *") title) 
    ((-gptr- "float *") v) 
    ((-int-) maxterms)  
    #{ ShowVector(  $title   ,$v   ,$maxterms  );  #} 
    ()
)


(de ShowDVector (  title  v  maxterms )
    ((-gptr- "char *") title) 
    ((-gptr- "double *") v) 
    ((-int-) maxterms)  
    #{ ShowDVector(  $title   ,$v   ,$maxterms  );  #} 
    ()
)


(de ZeroMatrix (  m )
    ((-gptr- "float **") m)  
    #{ ZeroMatrix(  $m  );  #} 
    ()
)


(de ZeroDMatrix (  m )
    ((-gptr- "double **") m)  
    #{ ZeroDMatrix(  $m  );  #} 
    ()
)


(de ZeroTriMat (  m )
    ((-gptr- "float **") m)  
    #{ ZeroTriMat(  $m  );  #} 
    ()
)


(de CopyMatrix (  m1  m2 )
    ((-gptr- "float **") m1) 
    ((-gptr- "float **") m2)  
    #{ CopyMatrix (  $m1   ,$m2  );  #} 
    ()
)


(de CopyDMatrix (  m1  m2 )
    ((-gptr- "double **") m1) 
    ((-gptr- "double **") m2)  
    #{ CopyDMatrix(  $m1   ,$m2  );  #} 
    ()
)


(de CopyTriMat ( m1  m2 )
    ((-gptr- "float **") m1) 
    ((-gptr- "float **") m2)  
    #{ CopyTriMat (  $m1   ,$m2  );  #} 
    ()
)


(de Mat2DMat (  m1  m2 )
    ((-gptr- "float **") m1) 
    ((-gptr- "double **") m2)  
    #{ Mat2DMat(  $m1   ,$m2  );  #} 
    ()
)


(de DMat2Mat (  m1  m2 )
    ((-gptr- "double **") m1) 
    ((-gptr- "float **") m2)  
    #{ DMat2Mat(  $m1   ,$m2  );  #} 
    ()
)


(de Mat2Tri (  m1  m2 )
    ((-gptr- "float **") m1) 
    ((-gptr- "float **") m2)  
    #{ Mat2Tri (  $m1   ,$m2  );  #} 
    ()
)


(de Tri2Mat (  m1  m2 )
    ((-gptr- "float **") m1) 
    ((-gptr- "float **") m2)  
    #{ Tri2Mat (  $m1   ,$m2  );  #} 
    ()
)


(de ReadMatrix (  src  m  binary )
    ((-gptr- "Source *") src) 
    ((-gptr- "float **") m) 
    ((-int-) binary)  
    (int   #{ ReadMatrix(  $src   ,$m   ,$binary  )  #} )
)


(de ReadTriMat (  src  m  binary )
    ((-gptr- "Source *") src) 
    ((-gptr- "float **") m) 
    ((-int-) binary)  
    (int   #{ ReadTriMat(  $src   ,$m   ,$binary  )  #} )
)


(de WriteMatrix (  f  m  binary )
    ((-gptr- "FILE *") f) 
    ((-gptr- "float **") m) 
    ((-int-) binary)  
    #{ WriteMatrix(  $f   ,$m   ,$binary  );  #} 
    ()
)


(de WriteTriMat (  f  m  binary )
    ((-gptr- "FILE *") f) 
    ((-gptr- "float **") m) 
    ((-int-) binary)  
    #{ WriteTriMat(  $f   ,$m   ,$binary  );  #} 
    ()
)


(de ShowMatrix (  title  m  maxcols  maxrows )
    ((-gptr- "char *") title) 
    ((-gptr- "float **") m) 
    ((-int-) maxcols) 
    ((-int-) maxrows)  
    #{ ShowMatrix(  $title   ,$m   ,$maxcols   ,$maxrows  );  #} 
    ()
)


(de ShowDMatrix (  title  m  maxcols  maxrows )
    ((-gptr- "char *") title) 
    ((-gptr- "double **") m) 
    ((-int-) maxcols) 
    ((-int-) maxrows)  
    #{ ShowDMatrix(  $title   ,$m   ,$maxcols   ,$maxrows  );  #} 
    ()
)


(de ShowTriMat (  title  m  maxcols  maxrows )
    ((-gptr- "char *") title) 
    ((-gptr- "float **") m) 
    ((-int-) maxcols) 
    ((-int-) maxrows)  
    #{ ShowTriMat(  $title   ,$m   ,$maxcols   ,$maxrows  );  #} 
    ()
)


(de CovInvert (  c  invc )
    ((-gptr- "float **") c) 
    ((-gptr- "float **") invc)  
    (double   #{ CovInvert(  $c   ,$invc  )  #} )         ;; float -> double
)


(de CovDet (  c )
    ((-gptr- "float **") c)  
    (double   #{ CovDet(  $c  )  #} )                     ;; float -> double
)


(de SVD (  a  u  v  d )
    ((-gptr- "double **") a) 
    ((-gptr- "double **") u) 
    ((-gptr- "double **") v) 
    ((-gptr- "double *") d)  
    #{ SVD(  $a   ,$u   ,$v   ,$d  );  #} 
    ()
)


(de InvSVD (  a  u  w  v  result )
    ((-gptr- "double **") a) 
    ((-gptr- "double **") u) 
    ((-gptr- "double *") w) 
    ((-gptr- "double **") v) 
    ((-gptr- "double **") result)  
    #{ InvSVD(  $a   ,$u   ,$w   ,$v   ,$result  );  #} 
    ()
)


(de LAdd (  x  y )
    ((-double-) x) 
    ((-double-) y)  
    (double   #{ LAdd(  $x   ,$y  )  #} )
)


(de LSub (  x  y )
    ((-double-) x) 
    ((-double-) y)  
    (double   #{ LSub(  $x   ,$y  )  #} )
)


(de L2F (  x )
    ((-double-) x)  
    (double   #{ L2F(  $x  )  #} )
)


(de RandInit (  seed )
    ((-int-) seed)  
    #{ RandInit(  $seed  );  #} 
    ()
)



(de RandomValue (  )
    (double   #{ RandomValue(  )  #} )                    ;; float -> double
)


(de GaussDeviate (  mu  sigma )
    ((-double-) mu)                         ;; float -> double
    ((-double-) sigma)                          ;; float -> double
    (double   #{ GaussDeviate(  $mu   ,$sigma  )  #} )         ;; float -> double
)


;; ********************************************************

(dhc-make-with-libs () (list htk-libhtk)
  dummy_htk_math
  initmath
  zeroshortvec
  zerointvec
  zerovector
  zerodvector
  copyshortvec
  copyintvec
  copyvector
  copydvector
  readshortvec
  readintvec
  readvector
  writeshortvec
  writeintvec
  writevector
  showshortvec
  showintvec
  showvector
  showdvector
  zeromatrix
  zerodmatrix
  zerotrimat
  copymatrix
  copydmatrix
  copytrimat
  mat2dmat
  dmat2mat
  mat2tri
  tri2mat
  readmatrix
  readtrimat
  writematrix
  writetrimat
  showmatrix
  showdmatrix
  showtrimat
  covinvert
  covdet
  svd
  invsvd
  ladd
  lsub
  l2f
  randinit
  randomvalue
  gaussdeviate
)
