1 /* GSequencer - Advanced GTK Sequencer
2 * Copyright (C) 2005-2020 Joël Krähemann
3 *
4 * This file is part of GSequencer.
5 *
6 * GSequencer is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GSequencer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GSequencer. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <ags/lib/ags_complex.h>
21
22 #include <stdlib.h>
23
24 /**
25 * SECTION:ags_complex
26 * @short_description: Boxed type of complex
27 * @title: AgsComplex
28 * @section_id:
29 * @include: ags/lib/ags_complex.h
30 *
31 * Boxed type of complex data type.
32 */
33
34 GType
ags_complex_get_type(void)35 ags_complex_get_type(void)
36 {
37 static volatile gsize g_define_type_id__volatile = 0;
38
39 if(g_once_init_enter (&g_define_type_id__volatile)){
40 GType ags_type_complex = 0;
41
42 ags_type_complex =
43 g_boxed_type_register_static("AgsComplex",
44 (GBoxedCopyFunc) ags_complex_copy,
45 (GBoxedFreeFunc) ags_complex_free);
46
47 g_once_init_leave(&g_define_type_id__volatile, ags_type_complex);
48 }
49
50 return g_define_type_id__volatile;
51 }
52
53 /**
54 * ags_complex_alloc:
55 *
56 * Allocate #AgsComplex-struct
57 *
58 * Returns: a new #AgsComplex-struct
59 *
60 * Since: 3.0.0
61 */
62 AgsComplex*
ags_complex_alloc()63 ags_complex_alloc()
64 {
65 AgsComplex *ptr;
66
67 ptr = (AgsComplex *) malloc(sizeof(AgsComplex));
68
69 ptr[0].real = 0.0;
70 ptr[0].imag = 0.0;
71
72 return(ptr);
73 }
74
75 /**
76 * ags_complex_copy:
77 * @ptr: the original #AgsComplex-struct
78 *
79 * Create a copy of @ptr.
80 *
81 * Returns: a pointer of the new #AgsComplex-struct
82 *
83 * Since: 3.0.0
84 */
85 gpointer
ags_complex_copy(AgsComplex * ptr)86 ags_complex_copy(AgsComplex *ptr)
87 {
88 AgsComplex *new_ptr;
89
90 new_ptr = (AgsComplex *) malloc(sizeof(AgsComplex));
91
92 new_ptr->real = ptr->real;
93 new_ptr->imag = ptr->imag;
94
95 return(new_ptr);
96 }
97
98 /**
99 * ags_complex_free:
100 * @ptr: the #AgsComplex-struct
101 *
102 * Free the memory of @ptr.
103 *
104 * Since: 3.0.0
105 */
106 void
ags_complex_free(AgsComplex * ptr)107 ags_complex_free(AgsComplex *ptr)
108 {
109 g_free(ptr);
110 }
111
112 /**
113 * ags_complex_get:
114 * @ptr: the #AgsComplex
115 *
116 * Get complex number.
117 *
118 * Returns: number as complex data type
119 *
120 * Since: 3.0.0
121 */
122 double complex
ags_complex_get(AgsComplex * ptr)123 ags_complex_get(AgsComplex *ptr)
124 {
125 double _Complex z;
126
127 z = ptr->real + I * ptr->imag;
128
129 return(z);
130 }
131
132 /**
133 * ags_complex_set:
134 * @ptr: the #AgsComplex-struct
135 * @z: the complex data to set
136 *
137 * Set complex number.
138 *
139 * Since: 3.0.0
140 */
141 void
ags_complex_set(AgsComplex * ptr,double _Complex z)142 ags_complex_set(AgsComplex *ptr, double _Complex z)
143 {
144 ptr->real = creal(z);
145 ptr->imag = cimag(z);
146 }
147
148 /**
149 * ags_complex_get:
150 * @ptr: the #AgsComplex-struct
151 * @real: (out): the real part
152 * @imag: (out): the imaginary part
153 *
154 * Get complex number.
155 *
156 * Returns: number as complex data type
157 *
158 * Since: 3.7.11
159 */
160 void
ags_complex_get_term(AgsComplex * ptr,gdouble * real,gdouble * imag)161 ags_complex_get_term(AgsComplex *ptr,
162 gdouble *real,
163 gdouble *imag)
164 {
165 if(real != NULL){
166 real[0] = ptr->real;
167 }
168
169 if(imag != NULL){
170 imag[0] = ptr->imag;
171 }
172 }
173
174 /**
175 * ags_complex_set:
176 * @ptr: the #AgsComplex-struct
177 * @real: the real part
178 * @imag: the imaginary part
179 *
180 * Set complex number.
181 *
182 * Since: 3.7.11
183 */
ags_complex_set_term(AgsComplex * ptr,gdouble real,gdouble imag)184 void ags_complex_set_term(AgsComplex *ptr,
185 gdouble real,
186 gdouble imag)
187 {
188 ptr->real = real;
189 ptr->imag = imag;
190 }
191