home *** CD-ROM | disk | FTP | other *** search
- 0.0 )
- sum.re = 1.0;
- else
- {
- sum.re += sum.re / norm1;
- sum.im += sum.im / norm1;
- }
- /* y->ve[i] = sum / QR->me[i][i]; */
- y->ve[i] = zdiv(sum,QR->me[i][i]);
- }
- zUAmlt(QR,y,y);
-
- /* no* indirect, special, incidental or consequential damages arising
- ** in any way from use of the software.
- **
- ** Everyone is granted permission to copy, modify and redistribute this
- ** Meschach Library, provided:
- ** 1. All copies contain this copyright notice.
- ** 2. All modified copies shall carry a notice stating who
- ** made the last modification and the date of such modification.
- ** 3. No charge is made for this software or works derived from it.
- ** This clause shall not be construed as constraining other software
- ** distributed on the same medium as this software, nor is a
- ** distribution fee considered a charge.
- **
- ***************************************************************************/
-
-
- /*
- This file contains routines for import/exporting complex data
- to/from MATLAB. The main routines are:
- ZMAT *zm_save(FILE *fp,ZMAT *A,char *name)
- ZVEC *zv_save(FILE *fp,ZVEC *x,char *name)
- complex z_save(FILE *fp,complex z,char *name)
- ZMAT *zm_load(FILE *fp,char **name)
- */
-
- #include <stdio.h>
- #include "zmatrix.h"
- #include "matlab.h"
-
- static char rcsid[] = "$Id: zmatlab.c,v 1.1 1994/01/13 04:24:57 des Exp $";
-
- /* zm_save -- save matrix in ".mat" file for MATLAB
- -- returns matrix to be saved */
- ZMAT *zm_save(fp,A,name)
- FILE *fp;
- ZMAT *A;
- char *name;
- {
- int i, j;
- matlab mat;
-
- if ( ! A )
- error(E_NULL,"zm_save");
-
- mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
- mat.m = A->m;
- mat.n = A->n;
- mat.imag = TRUE;
- mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
-
- /* write header */
- fwrite(&mat,sizeof(matlab),1,fp);
- /* write name */
- if ( name == (char *)NULL )
- fwrite("",sizeof(char),1,fp);
- else
- fwrite(name,sizeof(char),(int)(mat.namlen),fp);
- /* write actual data */
- for ( i = 0; i < A->m; i++ )
- for ( j = 0; j < A->n; j++ )
- fwrite(&(A->me[i][j].re),sizeof(Real),1,fp);
- for ( i = 0; i < A->m; i++ )
- for ( j = 0; j < A->n; j++ )
- fwrite(&(A->me[i][j].im),sizeof(Real),1,fp);
-
- return A;
- }
-
-
- /* zv_save -- save vector in ".mat" file for MATLAB
- -- saves it as a row vector
- -- returns vector to be saved */
- ZVEC *zv_save(fp,x,name)
- FILE *fp;
- ZVEC *x;
- char *name;
- {
- int i;
- matlab mat;
-
- if ( ! x )
- error(E_NULL,"zv_save");
-
- mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
- mat.m = x->dim;
- mat.n = 1;
- mat.imag = TRUE;
- mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
-
- /* write header */
- fwrite(&mat,sizeof(matlab),1,fp);
- /* write name */
- if ( name == (char *)NULL )
- fwrite("",sizeof(char),1,fp);
- else
- fwrite(name,sizeof(char),(int)(mat.namlen),fp);
- /* write actual data */
- for ( i = 0; i < x->dim; i++ )
- fwrite(&(x->ve[i].re),sizeof(Real),1,fp);
- for ( i = 0; i < x->dim; i++ )
- fwrite(&(x->ve[i].im),sizeof(Real),1,fp);
-
- return x;
- }
-
- /* z_save -- saves complex number in ".mat" file for MATLAB
- -- returns complex number to be saved */
- complex z_save(fp,z,name)
- FILE *fp;
- complex z;
- char *name;
- {
- matlab mat;
-
- mat.type = 1000*MACH_ID + 100*ORDER + 10*PRECISION + 0;
- mat.m = 1;
- mat.n = 1;
- mat.imag = TRUE;
- mat.namlen = (name == (char *)NULL) ? 1 : strlen(name)+1;
-
- /* write header */
- fwrite(&mat,sizeof(matlab),1,fp);
- /* write name */
- if ( name == (char *)NULL )
- fwrite("",sizeof(char),1,fp);
- else
- fwrite(name,sizeof(char),(int)(mat.namlen),fp);
- /* write actual data */
- fwrite(&z,sizeof(complex),1,fp);
-
- return z;
- }
-
-
-
- /* zm_load -- loads in a ".mat" file variable as produced by MATLAB
- -- matrix returned; imaginary parts ignored */
- ZMAT *zm_load(fp,name)
- FILE *fp;
- char **name;
- {
- ZMAT *A;
- int i;
- int m_flag, o_flag, p_flag, t_flag;
- float f_temp;
- double d_temp;
- matlab mat;
-
- if ( fread(&mat,sizeof(matlab),1,fp) != 1 )
- error(E_FORMAT,"zm_load");
- if ( mat.type >= 10000 ) /* don't load a sparse matrix! */
- error(E_FORMAT,"zm_load");
- m_flag = (mat.type/1000) % 10;
- o_flag = (mat.type/100) % 10;
- p_flag = (mat.type/10) % 10;
- t_flag = (mat.type) % 10;
- if ( m_flag != MACH_ID )
- error(E_FORMAT,"zm_load");
- if ( t_flag != 0 )
- error(E_FORMAT,"zm_load");
- if ( p_flag != DOUBLE_PREC && p_flag != SINGLE_PREC )
- error(E_FORMAT,"zm_load");
- *name = (char *)malloc((unsigned)(mat.namlen)+1);
- if ( fread(*name,sizeof(char),(unsigned)(mat.namlen),fp) == 0 )
- error(E_FORMAT,"zm_load");
- A = zm_get((unsigned)(mat.m),(unsigned)(mat.n));
- for ( i = 0; i < A->m*A->n; i++ )
- {
- if ( p_flag == DOUBLE_PREC )
- fread(&d_temp,sizeof(double),1,fp);
- else
- {
- fread(&f_temp,sizeof(float),1,fp);
- d_temp = f_temp;
- }
- if ( o_flag == ROW_ORDER )
- A->me[i / A->n][i % A->n].re = d_temp;
- else if ( o_flag == COL_ORDER )
- A->me[i % A->m][i / A->m].re = d_temp;
- else
- error(E_FORMAT,"zm_load");
- }
-
- if ( mat.imag ) /* skip imaginary part */
- for ( i = 0; i < A->m*A->n; i++ )
- {
- if ( p_flag == DOUBLE_PREC )
- fread(&d_temp,sizeof(double),1,fp);
- else
- {
- fread(&f_temp,sizeof(float),1,fp);
- d_temp = f_temp;
- }
- if ( o_flag == ROW_ORDER )
- A->me[i / A->n][i % A->n].im = d_temp;
- else if ( o_flag == COL_ORDER )
- A->me[i % A->m][i / A->m].im = d_temp;
- else
- error(E_FORMAT,"zm_load");
- }
-
- return A;
- }
-
- FileDataŵzmatop ”; Eýÿÿ õ
- /**************************************************************************
- **
- ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
- **
- ** Meschach Library
- **
- ** This Meschach Library is provided "as is" without any express
- ** or implied warranty of any kind with respect to this software.
- ** In particular the authors shall not be liable for any direct,
- ** indirect, special, incidental or consequential damages arising
- ** in any way from use of the software.
- **
- ** Everyone is granted permission to copy, modify and redistribute this
- ** Meschach Library, provided:
- ** 1. All copies contain this copyright notice.
- ** 2. All modified copies shall carry a notice stating who
- ** made the last modification and the date of such modification.
- ** 3. No charge is made for this software or works derived from it.
- ** This clause shall not be construed as constraining other software
- ** distributed on the same medium as this software, nor is a
- ** distribution fee considered a charge.
- **
- ***************************************************************************/
-
-
-
- #include <stdio.h>
- #include "zmatrix.h"
-
- static char rcsid[] = "$Id: zmatop.c,v 1.1 1994/01/13 04:24:46 des Exp $";
-
-
- #define is_zero(z) ((z).re == 0.0 && (z).im == 0.0)
-
- /* zm_add -- matrix addition -- may be in-situ */
- ZMAT *zm_add(mat1,mat2,out)
- ZMAT *mat1,*mat2,*out;
- {
- u_int m,n,i;
-
- if ( mat1==ZMNULL || mat2==ZMNULL )
- error(E_NULL,"zm_add");
- if ( mat1->m != mat2->m || mat1->n != mat2->n )
- error(E_SIZES,"zm_add");
- if ( out==ZMNULL || out->m != mat1->m || out->n != mat1->n )
- out = zm_resize(out,mat1->m,mat1->n);
- m = mat1->m; n = mat1->n;
- for ( i=0; i<m; i++ )
- {
- __zadd__(mat1->me[i],mat2->me[i],out->me[i],(int)n);
- /**************************************************
- for ( j=0; j<n; j++ )
- out->me[i][j] = mat1->me[i][j]+mat2->me[i][j];
- **************************************************/
- }
-
- return (out);
- }
-
- /* zm_sub -- matrix subtraction -- may be in-situ */
- ZMAT *zm_sub(mat1,mat2,out)
- ZMAT *mat1,*mat2,*out;
- {
- u_int m,n,i;
-
- if ( mat1==ZMNULL || mat2==ZMNULL )
- error(E_NULL,"zm_sub");
- if ( mat1->m != mat2->m || mat1->n != mat2->n )
- error(E_SIZES,"zm_sub");
- if ( out==ZMNULL || out->m != mat1->m || out->n != mat1->n )
- out = zm_resize(out,mat1->m,mat1->n);
- m = mat1->m; n = mat1->n;
- for ( i=0; i<m; i++ )
- {
- __zsub__(mat1->me[i],mat2->me[i],out->me[i],(int)n);
- /**************************************************
- for ( j=0; j<n; j++ )
- out->me[i][j] = mat1->me[i][j]-mat2->me[i][j];
- **************************************************/
- }
-
- return (out);
- }
-
- /*
- Note: In the following routines, "adjoint" means complex conjugate
- transpose:
- A* = conjugate(A^T)
- */
-
- /* zm_mlt -- matrix-matrix multiplication */
- ZMAT *zm_mlt(A,B,OUT)
- ZMAT *A,*B,*OUT;
- {
- u_int i, /* j, */ k, m, n, p;
- complex **A_v, **B_v /*, *B_row, *OUT_row, sum, tmp */;
-
- if ( A==ZMNULL || B==ZMNULL )
- error(E_NULL,"zm_mlt");
- if ( A->n != B->m )
- error(E_SIZES,"zm_mlt");
- if ( A == OUT || B == OUT )
- error(E_INSITU,"zm_mlt");
- m = A->m; n = A->n; p = B->n;
- A_v = A->me; B_v = B->me;
-
- if ( OUT==ZMNULL || OUT->m != A->m || OUT->n != B->n )
- OUT = zm_resize(OUT,A->m,B->n);
-
- /****************************************************************
- for ( i=0; i<m; i++ )
- for ( j=0; j<p; j++ )
- {
- sum = 0.0;
- for ( k=0; k<n; k++ )
- sum += A_v[i][k]*B_v[k][j];
- OUT->me[i][j] = sum;
- }
- ****************************************************************/
- zm_zero(OUT);
- for ( i=0; i<m; i++ )
- for ( k=0; k<n; k++ )
- {
- if ( ! is_zero(A_v[i][k]) )
- __zmltadd__(OUT->me[i],B_v[k],A_v[i][k],(int)p,Z_NOCONJ);
- /**************************************************
- B_row = B_v[k]; OUT_row = OUT->me[i];
- for ( j=0; j<p; j++ )
- (*OUT_row++) += tmp*(*B_row++);
- **************************************************/
- }
-
- return OUT;
- }
-
- /* zmma_mlt -- matrix-matrix adjoint multiplication
- -- A.B* is returned, and stored in OUT */
- ZMAT *zmma_mlt(A,B,OUT)
- ZMAT *A, *B, *OUT;
- {
- int i, j, limit;
- /* complex *A_row, *B_row, sum; */
-
- if ( ! A || ! B )
- error(E_NULL,"zmma_mlt");
- if ( A == OUT || B == OUT )
- error(E_INSITU,"zmma_mlt");
- if ( A->n != B->n )
- error(E_SIZES,"zmma_mlt");
- if ( ! OUT || OUT->m != A->m || OUT->n != B->m )
- OUT = zm_resize(OUT,A->m,B->m);
-
- limit = A->n;
- for ( i = 0; i < A->m; i++ )
- for ( j = 0; j < B->m; j++ )
- {
- OUT->me[i][j] = __zip__(B->me[j],A->me[i],(int)limit,Z_CONJ);
- /**************************************************
- sum = 0.0;
- A_row = A->me[i];
- B_row = B->me[j];
- for ( k = 0; k < limit; k++ )
- sum += (*A_row++)*(*B_row++);
- OUT->me[i][j] = sum;
- **************************************************/
- }
-
- return OUT;
- }
-
- /* zmam_mlt -- matrix adjoint-matrix multiplication
- -- A*.B is returned, result stored in OUT */
- ZMAT *zmam_mlt(A,B,OUT)
- ZMAT *A, *B, *OUT;
- {
- int i, k, limit;
- /* complex *B_row, *OUT_row, multiplier; */
- complex tmp;
-
- if ( ! A || ! B )
- error(E_NULL,"zmam_mlt");
- if ( A == OUT || B == OUT )
- error(E_INSITU,"zmam_mlt");
- if ( A->m != B->m )
- error(E_SIZES,"zmam_mlt");
- if ( ! OUT || OUT->m != A->n || OUT->n != B->n )
- OUT = zm_resize(OUT,A->n,B->n);
-
- limit = B->n;
- zm_zero(OUT);
- for ( k = 0; k < A->m; k++ )
- for ( i = 0; i < A->n; i++ )
- {
- tmp.re = A->me[k][i].re;
- tmp.im = - A->me[k][i].im;
- if ( ! is_zero(tmp) )
- __zmltadd__(OUT->me[i],B->me[k],tmp,(int)limit,Z_NOCONJ);
- }
-
- return OUT;
- }
-
- /* zmv_mlt -- matrix-vector multiplication
- -- Note: b is treated as a column vector */
- ZVEC *zmv_mlt(A,b,out)
- ZMAT *A;
- ZVEC *b,*out;
- {
- u_int i, m, n;
- complex **A_v, *b_v /*, *A_row */;
- /* register complex sum; */
-
- if ( A==ZMNULL || b==ZVNULL )
- error(E_NULL,"zmv_mlt");
- if ( A->n != b->dim )
- error(E_SIZES,"zmv_mlt");
- if ( b == out )
- error(E_INSITU,"zmv_mlt");
- if ( out == ZVNULL || out->dim != A->m )
- out = zv_resize(out,A->m);
-
- m = A->m; n = A->n;
- A_v = A->me; b_v = b->ve;
- for ( i=0; i<m; i++ )
- {
- /* for ( j=0; j<n; j++ )
- sum += A_v[i][j]*b_v[j]; */
- out->ve[i] = __zip__(A_v[i],b_v,(int)n,Z_NOCONJ);
- /**************************************************
- A_row = A_v[i]; b_v = b->ve;
- for ( j=0; j<n; j++ )
- sum += (*A_row++)*(*b_v++);
- out->ve[i] = sum;
- **************************************************/
- }
-
- return out;
- }
-
- /* zsm_mlt -- scalar-matrix multiply -- may be in-situ */
- ZMAT *zsm_mlt(scalar,matrix,out)
- complex scalar;
- ZMAT *matrix,*out;
- {
- u_int m,n,i;
-
- if ( matrix==ZMNULL )
- error(E_NULL,"zsm_mlt");
- if ( out==ZMNULL || out->m != matrix->m || out->n != matrix->n )
- out = zm_resize(out,matrix->m,matrix->n);
- m = matrix->m; n = matrix->n;
- for ( i=0; i<m; i++ )
- __zmlt__(matrix->me[i],scalar,out->me[i],(int)n);
- /**************************************************
- for ( j=0; j<n; j++ )
- out->me[i][j] = scalar*matrix->me[i][j];
- **************************************************/
- return (out);
- }
-
- /* zvm_mlt -- vector adjoint-matrix multiplication */
- ZVEC *zvm_mlt(A,b,out)
- ZMAT *A;
- ZVEC *b,*out;
- {
- u_int j,m,n;
- /* complex sum,**A_v,*b_v; */
-
- if ( A==ZMNULL || b==ZVNULL )
- error(E_NULL,"zvm_mlt");
- if ( A->m != b->dim )
- error(E_SIZES,"zvm_mlt");
- if ( b == out )
- error(E_INSITU,"zvm_mlt");
- if ( out == ZVNULL || out->dim != A->n )
- out = zv_resize(out,A->n);
-
- m = A->m; n = A->n;
-
- zv_zero(out);
- for ( j = 0; j < m; j++ )
- if ( b->ve[j].re != 0.0 || b->ve[j].im != 0.0 )
- __zmltadd__(out->ve,A->me[j],b->ve[j],(int)n,Z_CONJ);
- /**************************************************
- A_v = A->me; b_v = b->ve;
- for ( j=0; j<n; j++ )
- {
- sum = 0.0;
- for ( i=0; i<m; i++ )
- sum += b_v[