1 #include "cs.h"
2
3 /* convert from complex to real (int version) */
4 /* C = real(A) if real is true, imag(A) otherwise */
cs_i_real(cs_ci * A,int real)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 */
cs_i_complex(cs_di * A,int real)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 */
cs_l_real(cs_cl * A,cs_long_t real)51 cs_dl *cs_l_real (cs_cl *A, cs_long_t real)
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 */
cs_l_complex(cs_dl * A,cs_long_t real)74 cs_cl *cs_l_complex (cs_dl *A, cs_long_t real)
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 }
94