EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
cs_convert.c
Go to the documentation of this file.
1 #include "cs.h"
2 
3 /* convert from complex to real (int version) */
4 /* C = real(A) if real is true, imag(A) otherwise */
5 cs_di *cs_i_real (cs_ci *A, int real)
6 {
7  cs_di *C ;
8  int n, triplet, nn, p, nz, *Ap, *Ai, *Cp, *Ci ;
9  cs_complex_t *Ax ;
10  double *Cx ;
11  if (!A || !A->x) return (NULL) ; /* return if A NULL or pattern-only */
12  n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
13  triplet = (A->nz >= 0) ; /* true if A is a triplet matrix */
14  nz = triplet ? A->nz : Ap [n] ;
15  C = cs_di_spalloc (A->m, n, A->nzmax, 1, triplet) ;
16  if (!C) return (NULL) ;
17  Cp = C->p ; Ci = C->i ; Cx = C->x ;
18  nn = triplet ? nz : (n+1) ;
19  for (p = 0 ; p < nz ; p++) Ci [p] = Ai [p] ;
20  for (p = 0 ; p < nn ; p++) Cp [p] = Ap [p] ;
21  for (p = 0 ; p < nz ; p++) Cx [p] = real ? creal (Ax [p]) : cimag (Ax [p]) ;
22  if (triplet) C->nz = nz ;
23  return (C) ;
24 }
25 
26 /* convert from real to complex (int version) */
27 /* C = A if real is true, or C = i*A otherwise */
28 cs_ci *cs_i_complex (cs_di *A, int real)
29 {
30  cs_ci *C ;
31  int n, triplet, nn, p, nz, *Ap, *Ai, *Cp, *Ci ;
32  double *Ax ;
33  cs_complex_t *Cx ;
34  if (!A || !A->x) return (NULL) ; /* return if A NULL or pattern-only */
35  n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
36  triplet = (A->nz >= 0) ; /* true if A is a triplet matrix */
37  nz = triplet ? A->nz : Ap [n] ;
38  C = cs_ci_spalloc (A->m, n, A->nzmax, 1, triplet) ;
39  if (!C) return (NULL) ;
40  Cp = C->p ; Ci = C->i ; Cx = C->x ;
41  nn = triplet ? nz : (n+1) ;
42  for (p = 0 ; p < nz ; p++) Ci [p] = Ai [p] ;
43  for (p = 0 ; p < nn ; p++) Cp [p] = Ap [p] ;
44  for (p = 0 ; p < nz ; p++) Cx [p] = real ? Ax [p] : (I * Ax [p]) ;
45  if (triplet) C->nz = nz ;
46  return (C) ;
47 }
48 
49 /* convert from complex to real (cs_long_t version) */
50 /* C = real(A) if real is true, imag(A) otherwise */
52 {
53  cs_dl *C ;
54  cs_long_t n, triplet, nn, p, nz, *Ap, *Ai, *Cp, *Ci ;
55  cs_complex_t *Ax ;
56  double *Cx ;
57  if (!A || !A->x) return (NULL) ; /* return if A NULL or pattern-only */
58  n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
59  triplet = (A->nz >= 0) ; /* true if A is a triplet matrix */
60  nz = triplet ? A->nz : Ap [n] ;
61  C = cs_dl_spalloc (A->m, n, A->nzmax, 1, triplet) ;
62  if (!C) return (NULL) ;
63  Cp = C->p ; Ci = C->i ; Cx = C->x ;
64  nn = triplet ? nz : (n+1) ;
65  for (p = 0 ; p < nz ; p++) Ci [p] = Ai [p] ;
66  for (p = 0 ; p < nn ; p++) Cp [p] = Ap [p] ;
67  for (p = 0 ; p < nz ; p++) Cx [p] = real ? creal (Ax [p]) : cimag (Ax [p]) ;
68  if (triplet) C->nz = nz ;
69  return (C) ;
70 }
71 
72 /* convert from real to complex (cs_long_t version) */
73 /* C = A if real is true, or C = i*A otherwise */
75 {
76  cs_cl *C ;
77  cs_long_t n, triplet, nn, p, nz, *Ap, *Ai, *Cp, *Ci ;
78  double *Ax ;
79  cs_complex_t *Cx ;
80  if (!A || !A->x) return (NULL) ; /* return if A NULL or pattern-only */
81  n = A->n ; Ap = A->p ; Ai = A->i ; Ax = A->x ;
82  triplet = (A->nz >= 0) ; /* true if A is a triplet matrix */
83  nz = triplet ? A->nz : Ap [n] ;
84  C = cs_cl_spalloc (A->m, n, A->nzmax, 1, triplet) ;
85  if (!C) return (NULL) ;
86  Cp = C->p ; Ci = C->i ; Cx = C->x ;
87  nn = triplet ? nz : (n+1) ;
88  for (p = 0 ; p < nz ; p++) Ci [p] = Ai [p] ;
89  for (p = 0 ; p < nn ; p++) Cp [p] = Ap [p] ;
90  for (p = 0 ; p < nz ; p++) Cx [p] = real ? Ax [p] : (I * Ax [p]) ;
91  if (triplet) C->nz = nz ;
92  return (C) ;
93 }
int m
Definition: cs.h:66
int * p
Definition: cs.h:342
cs_long_t n
Definition: cs.h:476
cs_complex_t * x
Definition: cs.h:344
cs_long_t nz
Definition: cs.h:205
cs_complex_t * x
Definition: cs.h:479
int m
Definition: cs.h:340
double * x
Definition: cs.h:70
#define cs_complex_t
Definition: cs.h:41
cs_cl * cs_cl_spalloc(cs_long_t m, cs_long_t n, cs_long_t nzmax, cs_long_t values, cs_long_t t)
cs_cl * cs_l_complex(cs_dl *A, cs_long_t real)
Definition: cs_convert.c:74
cs_long_t * p
Definition: cs.h:477
int n
Definition: cs.h:67
cs_long_t m
Definition: cs.h:200
int nz
Definition: cs.h:345
cs_long_t nzmax
Definition: cs.h:474
cs_long_t * i
Definition: cs.h:203
cs_ci * cs_ci_spalloc(int m, int n, int nzmax, int values, int t)
int * i
Definition: cs.h:69
double * x
Definition: cs.h:204
cs_long_t nz
Definition: cs.h:480
int * i
Definition: cs.h:343
int nz
Definition: cs.h:71
int nzmax
Definition: cs.h:339
cs_long_t * i
Definition: cs.h:478
cs_di * cs_i_real(cs_ci *A, int real)
Definition: cs_convert.c:5
cs_dl * cs_l_real(cs_cl *A, cs_long_t real)
Definition: cs_convert.c:51
cs_long_t m
Definition: cs.h:475
int n
Definition: cs.h:341
cs_long_t nzmax
Definition: cs.h:199
int nzmax
Definition: cs.h:65
cs_ci * cs_i_complex(cs_di *A, int real)
Definition: cs_convert.c:28
cs_long_t n
Definition: cs.h:201
#define cs_long_t
Definition: cs.h:53
int * p
Definition: cs.h:68
cs_dl * cs_dl_spalloc(cs_long_t m, cs_long_t n, cs_long_t nzmax, cs_long_t values, cs_long_t t)
cs_di * cs_di_spalloc(int m, int n, int nzmax, int values, int t)
cs_long_t * p
Definition: cs.h:202