\File{expect.c},{00:02},{Dec 28 1997}
\L{\LB{\C{}\/* \$Source: \/mit\/bert\/Cvsroot\/Thesis\/src\/exact\/expect.c,v \$}}
\L{\LB{ *}}
\L{\LB{ * calculation of probabilities and expectation values}}
\L{\LB{ *\/\CE{}}}
\L{\LB{}}
\L{\LB{\K{\#include} \S{}\"complex.h\"\SE{}}}
\L{\LB{\K{\#include} \S{}\"expect.h\"\SE{}}}
\L{\LB{}}
\L{\LB{\K{static} \K{char} rcsid[] = \S{}\"\$Id: expect.c,v 1.9 1997\/12\/28 05:02:56 bert Exp \$\"\SE{};}}
\L{\LB{}}
\L{\LB{\C{}\/** code **\/\CE{}}}
\L{\LB{}}
\L{\LB{\C{}\/* calculate \<x\> of a packet}}
\L{\LB{ *}}
\L{\LB{ * arguments:}}
\L{\LB{ *   K \-\-       packet.}}
\L{\LB{ *   n \-\-       dimension of matrix.  (must be a power of 2 because of FFT.)}}
\L{\LB{ *   delta \-\-   discrete space step.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} expect\_X (\K{complex} *pack, \K{long} n, \K{double} delta)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,ii,ni,k,nk;}}
\L{\LB{}}
\L{\LB{  \K{real} result = 0.;}}
\L{\LB{}}
\L{\LB{  \C{}\/* it\'s easiest to do positives and negatives together. *\/\CE{}}}
\L{\LB{  \K{for} (i=0,ii=0,ni=0; ii\<=ni; i++,ii+=n,ni=(n*n)\-ii) \{}}
\L{\LB{    \K{for} (k=0,nk=0; k\<=nk; k++,nk=n\-k) \{}}
\L{\LB{      \K{if} (ni) \{}}
\L{\LB{}\Tab{8}{\K{if} (nk)}}
\L{\LB{}\Tab{8}{  result += i * ( Csqabs( pack[ii+k] ) + Csqabs( pack[ii+nk] )}}
\L{\LB{}\Tab{24}{ \- Csqabs( pack[ni+k] ) \- Csqabs( pack[ni+nk] ) );}}
\L{\LB{}\Tab{8}{\K{else}}}
\L{\LB{}\Tab{8}{  result += i * ( Csqabs( pack[ii+k] ) \- Csqabs( pack[ni+k] ) );}}
\L{\LB{      \}}}
\L{\LB{      \C{}\/* if ni=0, then i=0, so contribution is 0 *\/\CE{}}}
\L{\LB{    \}}}
\L{\LB{  \}}}
\L{\LB{  \K{return} (delta*result);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* calculate \<y\> of a packet}}
\L{\LB{ *}}
\L{\LB{ * arguments:}}
\L{\LB{ *   K \-\-       packet.}}
\L{\LB{ *   n \-\-       dimension of matrix.  (must be a power of 2 because of FFT.)}}
\L{\LB{ *   delta \-\-   discrete space step.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} expect\_Y (\K{complex} *pack, \K{long} n, \K{double} delta)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,ii,ni,k,nk;}}
\L{\LB{}}
\L{\LB{  \K{real} result = 0.;}}
\L{\LB{}}
\L{\LB{  \C{}\/* it\'s easiest to do positives and negatives together. *\/\CE{}}}
\L{\LB{  \K{for} (i=0,ii=0,ni=0; ii\<=ni; i++,ii+=n,ni=(n*n)\-ii) \{}}
\L{\LB{    \K{for} (k=0,nk=0; k\<=nk; k++,nk=n\-k) \{}}
\L{\LB{      \K{if} (nk) \{}}
\L{\LB{}\Tab{8}{\K{if} (ni)}}
\L{\LB{}\Tab{8}{  result += k * ( Csqabs( pack[ii+k] ) \- Csqabs( pack[ii+nk] )}}
\L{\LB{}\Tab{24}{ + Csqabs( pack[ni+k] ) \- Csqabs( pack[ni+nk] ) );}}
\L{\LB{}\Tab{8}{\K{else}}}
\L{\LB{}\Tab{8}{  result += k * ( Csqabs( pack[ii+k] ) \- Csqabs( pack[ii+nk] ) );}}
\L{\LB{      \}}}
\L{\LB{      \C{}\/* if nk=0, then k=0, so contribution is 0 *\/\CE{}}}
\L{\LB{}}
\L{\LB{    \}}}
\L{\LB{  \}}}
\L{\LB{  \K{return} (delta*result);}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* calculate sum of squares of absolute values of the matrix elements}}
\L{\LB{ * (For wavefunctions, this is the total probability.)}}
\L{\LB{ *   A \-\-    data.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} sum\_sq(\K{complex} *A, \K{long} n)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i;}}
\L{\LB{  \K{real} sum = 0;}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<(n*n); i++)}}
\L{\LB{    sum += Csqabs(A[i]);}}
\L{\LB{}}
\L{\LB{  \K{return} sum;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* calculate maximum of squares of absolute values of the matrix elements}}
\L{\LB{ * (For wavefunctions, this is the maximum probability density.)}}
\L{\LB{ *   A \-\-    data.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{real} max\_sq(\K{complex} *A, \K{long} n)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i;}}
\L{\LB{  \K{real} curr;}}
\L{\LB{  \K{real} max = 0;}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<(n*n); i++) \{}}
\L{\LB{    curr = Csqabs(A[i]);}}
\L{\LB{    \K{if} (max \< curr)}}
\L{\LB{      max = curr;}}
\L{\LB{  \}}}
\L{\LB{}}
\L{\LB{  \K{return} max;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* calculate overlap of two wave packets.}}
\L{\LB{ *   A \-\-    first packet.  (n x n array)}}
\L{\LB{ *   B \-\-    second packet.  (n x n array)}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{complex} overlap(\K{complex} *A, \K{complex} *B, \K{long} n)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i,j;}}
\L{\LB{  \K{complex} sum = 0.;}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<(n*n); i+=n)}}
\L{\LB{    \K{for} (j=0; j\<n; j++)}}
\L{\LB{      sum += (conj(A[i+j])*(B[i+j]));}}
\L{\LB{}}
\L{\LB{  \K{return} sum;}}
\L{\LB{\}}}
\L{\LB{}}
\L{\LB{\C{}\/* Normalize a wave function.}}
\L{\LB{ *   A \-\-    data.  (n x n array)  Contents is modified!}}
\L{\LB{ *   n \-\-    array size.}}
\L{\LB{ *\/\CE{}}}
\L{\LB{\K{void} normalize(\K{complex} *A, \K{long} n)}}
\L{\LB{\{}}
\L{\LB{  \K{long} i;}}
\L{\LB{  \K{real} sum = 0;}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<(n*n); i++)}}
\L{\LB{    sum += Csqabs(A[i]);}}
\L{\LB{}}
\L{\LB{  sum = sqrt(sum);}}
\L{\LB{}}
\L{\LB{  \K{for} (i=0; i\<(n*n); i++)}}
\L{\LB{    A[i] = A[i]\/sum;}}
\L{\LB{\}}}
