JWS C Library
C language utility library
blas.c
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00053 #include <jwsc/config.h>
00054 
00055 #include <stdlib.h>
00056 #include <assert.h>
00057 
00058 #include <Accelerate/Accelerate.h>
00059 
00060 #include "jwsc/math/complex.h"
00061 #include "jwsc/math/blas.h"
00062 
00063 
00064 /* ===============  LEVEL 1 ==============*/
00065 
00066 
00074 void blas_sswap(int n, float* x, int inc_x, float* y, int inc_y)
00075 {
00076     cblas_sswap(n, x, inc_x, y, inc_y);
00077 }
00078 
00079 void blas_dswap(int n, double* x, int inc_x, double* y, int inc_y)
00080 {
00081     cblas_dswap(n, x, inc_x, y, inc_y);
00082 }
00083 
00084 void blas_cswap(int n, Complex_f* x, int inc_x, Complex_f* y, int inc_y)
00085 {
00086     cblas_cswap(n, x, inc_x, y, inc_y);
00087 }
00088 
00089 void blas_zswap(int n, Complex_d* x, int inc_x, Complex_d* y, int inc_y)
00090 {
00091     cblas_zswap(n, x, inc_x, y, inc_y);
00092 }
00093 
00106 void blas_sscal(int n, float a, float* x, int inc_x)
00107 {
00108     cblas_sscal(n, a, x, inc_x);
00109 }
00110 
00111 void blas_dscal(int n, double a, double* x, int inc_x)
00112 {
00113     cblas_dscal(n, a, x, inc_x);
00114 }
00115 
00116 void blas_cscal(int n, Complex_f a, Complex_f* x, int inc_x)
00117 {
00118     cblas_cscal(n, &a, x, inc_x);
00119 }
00120 
00121 void blas_zscal(int n, Complex_d a, Complex_d* x, int inc_x)
00122 {
00123     cblas_zscal(n, &a, x, inc_x);
00124 }
00125 
00138 void blas_scopy(int n, const float* x, int inc_x, float* y, int inc_y)
00139 {
00140     cblas_scopy(n, (float*)x, inc_x, y, inc_y);
00141 }
00142 
00143 void blas_dcopy(int n, const double* x, int inc_x, double* y, int inc_y)
00144 {
00145     cblas_dcopy(n, (double*)x, inc_x, y, inc_y);
00146 }
00147 
00148 void blas_ccopy(int n, const Complex_f* x, int inc_x, Complex_f* y, int inc_y)
00149 {
00150     cblas_ccopy(n, (Complex_f*)x, inc_x, y, inc_y);
00151 }
00152 
00153 void blas_zcopy(int n, const Complex_d* x, int inc_x, Complex_d* y, int inc_y)
00154 {
00155     cblas_zcopy(n, (Complex_d*)x, inc_x, y, inc_y);
00156 }
00157 
00170 void blas_saxpy
00171 (
00172     int          n, 
00173     float        a, 
00174     const float* x, 
00175     int          inc_x, 
00176     float*       y, 
00177     int          inc_y
00178 )
00179 {
00180     cblas_saxpy(n, a, (float*)x, inc_x, y, inc_y);
00181 }
00182 
00183 void blas_daxpy
00184 (
00185     int           n, 
00186     double        a, 
00187     const double* x, 
00188     int           inc_x, 
00189     double*       y, 
00190     int           inc_y
00191 )
00192 {
00193     cblas_daxpy(n, a, (double*)x, inc_x, y, inc_y);
00194 }
00195 
00196 void blas_caxpy
00197 (
00198     int              n, 
00199     Complex_f        a, 
00200     const Complex_f* x, 
00201     int              inc_x, 
00202     Complex_f*       y, 
00203     int              inc_y
00204 )
00205 {
00206     cblas_caxpy(n, &a, (Complex_f*)x, inc_x, y, inc_y);
00207 }
00208 
00209 void blas_zaxpy
00210 (
00211     int              n, 
00212     Complex_d        a, 
00213     const Complex_d* x, 
00214     int              inc_x, 
00215     Complex_d*       y, 
00216     int              inc_y
00217 )
00218 {
00219     cblas_zaxpy(n, &a, (Complex_d*)x, inc_x, y, inc_y);
00220 }
00221 
00234 float blas_sdot(int n, const float* x, int inc_x, const float* y, int inc_y)
00235 {
00236     return cblas_sdot(n, (float*)x, inc_x, (float*)y, inc_y);
00237 }
00238 
00239 double blas_ddot(int n, const double* x, int inc_x, const double* y, int inc_y)
00240 {
00241     return cblas_ddot(n, (double*)x, inc_x, (double*)y, inc_y);
00242 }
00243 
00256 float blas_snrm2(int n, const float* x, int inc_x)
00257 {
00258     return cblas_snrm2(n, (float*)x, inc_x);
00259 }
00260 
00261 double blas_dnrm2(int n, const double* x, int inc_x)
00262 {
00263     return cblas_dnrm2(n, (double*)x, inc_x);
00264 }
00265 
00278 float blas_sasum(int n, const float* x, int inc_x)
00279 {
00280     return cblas_sasum(n, (float*)x, inc_x);
00281 }
00282 
00283 double blas_dasum(int n, const double* x, int inc_x)
00284 {
00285     return cblas_dasum(n, (double*)x, inc_x);
00286 }
00287 
00300 int blas_isamax(int n, const float* x, int inc_x)
00301 {
00302     return cblas_isamax(n, (float*)x, inc_x);
00303 }
00304 
00305 int blas_idamax(int n, const double* x, int inc_x)
00306 {
00307     return cblas_idamax(n, (double*)x, inc_x);
00308 }
00309 
00310 int blas_icamax(int n, const Complex_f* x, int inc_x)
00311 {
00312     return cblas_icamax(n, (Complex_f*)x, inc_x);
00313 }
00314 
00315 int blas_izamax(int n, const Complex_d* x, int inc_x)
00316 {
00317     return cblas_izamax(n, (Complex_d*)x, inc_x);
00318 }
00319 
00325 /* ===============  LEVEL 2 PROTOTYPES ==============*/
00326 
00327 
00358 void blas_sgemv
00359 (
00360     char         trans, 
00361     int          m, 
00362     int          n, 
00363     float        alpha, 
00364     const float* A, 
00365     int          lda,
00366     const float* x,
00367     int          inc_x,
00368     float        beta,
00369     float*       y,
00370     int          inc_y
00371 )
00372 {
00373     trans = (trans == 'N' || trans == 'n') ? CblasTrans : CblasNoTrans;
00374     cblas_sgemv(CblasColMajor, trans, n, m, alpha, (float*)A, lda, (float*)x,
00375             inc_x, beta, y, inc_y);
00376 }
00377 
00401 void blas_dgemv
00402 (
00403     char          trans, 
00404     int           m, 
00405     int           n, 
00406     double        alpha, 
00407     const double* A, 
00408     int           lda,
00409     const double* x,
00410     int           inc_x,
00411     double        beta,
00412     double*       y,
00413     int           inc_y
00414 )
00415 {
00416     trans = (trans == 'N' || trans == 'n') ? CblasTrans : CblasNoTrans;
00417     cblas_dgemv(CblasColMajor, trans, n, m, alpha, (double*)A, lda, (double*)x,
00418             inc_x, beta, y, inc_y);
00419 }
00420 
00444 void blas_cgemv
00445 (
00446     char             trans, 
00447     int              m, 
00448     int              n, 
00449     Complex_f        alpha, 
00450     const Complex_f* A, 
00451     int              lda,
00452     const Complex_f* x,
00453     int              inc_x,
00454     Complex_f        beta,
00455     Complex_f*       y,
00456     int              inc_y
00457 )
00458 {
00459     trans = (trans == 'N' || trans == 'n') ? CblasTrans : CblasNoTrans;
00460     cblas_cgemv(CblasColMajor, trans, n, m, &alpha, (Complex_f*)A, lda,
00461             (Complex_f*)x, inc_x, &beta, y, inc_y);
00462 }
00463 
00487 void blas_zgemv
00488 (
00489     char             trans, 
00490     int              m, 
00491     int              n, 
00492     Complex_d        alpha, 
00493     const Complex_d* A, 
00494     int              lda,
00495     const Complex_d* x,
00496     int              inc_x,
00497     Complex_d        beta,
00498     Complex_d*       y,
00499     int              inc_y
00500 )
00501 {
00502     trans = (trans == 'N' || trans == 'n') ? CblasTrans : CblasNoTrans;
00503     cblas_zgemv(CblasColMajor, trans, n, m, &alpha, (Complex_d*)A, lda,
00504             (Complex_d*)x, inc_x, &beta, y, inc_y);
00505 }
00506 
00512 /* ===============  LEVEL 3 ==============*/
00513 
00514 
00553 void blas_sgemm
00554 (
00555     char         trans_a, 
00556     char         trans_b, 
00557     int          m, 
00558     int          n, 
00559     int          k, 
00560     float        alpha, 
00561     const float* A, 
00562     int          lda,
00563     const float* B,
00564     int          ldb,
00565     float        beta,
00566     float*       C,
00567     int          ldc
00568 )
00569 {
00570     trans_a = (trans_a == 'N' || trans_a == 'n') ? CblasNoTrans : CblasTrans;
00571     trans_b = (trans_b == 'N' || trans_b == 'n') ? CblasNoTrans : CblasTrans;
00572     cblas_sgemm(CblasColMajor, trans_a, trans_b, n, m, k, alpha, (float*) B,
00573             ldb, (float*) A, lda, beta, C, ldc);
00574 }
00575 
00607 void blas_dgemm
00608 (
00609     char          trans_a, 
00610     char          trans_b, 
00611     int           m, 
00612     int           n, 
00613     int           k, 
00614     double        alpha, 
00615     const double* A, 
00616     int           lda,
00617     const double* B,
00618     int           ldb,
00619     double        beta,
00620     double*       C,
00621     int           ldc
00622 )
00623 {
00624     trans_a = (trans_a == 'N' || trans_a == 'n') ? CblasNoTrans : CblasTrans;
00625     trans_b = (trans_b == 'N' || trans_b == 'n') ? CblasNoTrans : CblasTrans;
00626     cblas_dgemm(CblasColMajor, trans_a, trans_b, n, m, k, alpha, (double*) B,
00627             ldb, (double*) A, lda, beta, C, ldc);
00628 }
00629 
00661 void blas_cgemm
00662 (
00663     char             trans_a, 
00664     char             trans_b, 
00665     int              m, 
00666     int              n, 
00667     int              k, 
00668     Complex_f        alpha, 
00669     const Complex_f* A, 
00670     int              lda,
00671     const Complex_f* B,
00672     int              ldb,
00673     Complex_f        beta,
00674     Complex_f*       C,
00675     int              ldc
00676 )
00677 {
00678     trans_a = (trans_a == 'N' || trans_a == 'n') ? CblasNoTrans : CblasTrans;
00679     trans_b = (trans_b == 'N' || trans_b == 'n') ? CblasNoTrans : CblasTrans;
00680     cblas_cgemm(CblasColMajor, trans_a, trans_b, n, m, k, &alpha, (Complex_f*)
00681             B, ldb, (Complex_f*) A, lda, &beta, C, ldc);
00682 }
00683 
00715 void blas_zgemm
00716 (
00717     char             trans_a, 
00718     char             trans_b, 
00719     int              m, 
00720     int              n, 
00721     int              k, 
00722     Complex_d        alpha, 
00723     const Complex_d* A, 
00724     int              lda,
00725     const Complex_d* B,
00726     int              ldb,
00727     Complex_d        beta,
00728     Complex_d*       C,
00729     int              ldc
00730 )
00731 {
00732     trans_a = (trans_a == 'N' || trans_a == 'n') ? CblasNoTrans : CblasTrans;
00733     trans_b = (trans_b == 'N' || trans_b == 'n') ? CblasNoTrans : CblasTrans;
00734     cblas_zgemm(CblasColMajor, trans_a, trans_b, n, m, k, &alpha, (Complex_d*)
00735             B, ldb, (Complex_d*) A, lda, &beta, C, ldc);
00736 }
00737