Debian Codegraph @

src/core/ulib_math.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: ulib_math.cpp
28 // desc: class library for 'Math'
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Spring 2004
32 //-----------------------------------------------------------------------------
33 #include "ulib_math.h"
34 #include "ulib_std.h"
35 #include "chuck_type.h"
36 #include "chuck_compile.h"
37 #include "util_math.h"
38
39 #include <float.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <limits.h>
43
44 #include <vector> // 1.5.0.0 (ge) | added
45 #include <limits> // 1.5.2.2 (nick) | added
46
47
48 static double g_pi = CK_ONE_PI;
49 static double g_twopi = CK_TWO_PI;
50 static double g_e = ::exp( 1.0 );
51 static t_CKFLOAT g_floatMax = CK_FLT_MAX;
52 static t_CKFLOAT g_floatMin = CK_FLT_MIN;
53 static t_CKFLOAT g_inf = 0.0;
54 static t_CKINT g_intMax = CK_INT_MAX;
55 static t_CKINT g_randomMax = CK_RANDOM_MAX;
56 static t_CKCOMPLEX g_i = { 0.0, 1.0 };
57 static t_CKFLOAT fzero() { return 0.0; }
58
59
60 // query
61 DLL_QUERY libmath_query( Chuck_DL_Query * QUERY )
62 {
63 // get global
64 Chuck_Env * env = QUERY->env();
65 // name
66 QUERY->setname( QUERY, "Math" );
67
68 /*! \example
69 math.sin( math.pi /2.0 ) => stdout;
70 */
71
72 // register deprecate
73 type_engine_register_deprecate( env, "math", "Math" );
74
75 // add class
76 QUERY->begin_class( QUERY, "Math", "Object" );
77 // add documentatiopn
78 QUERY->doc_class( QUERY, "math class library." );
79
80 // add examples | 1.5.0.4 (ge) added
81 QUERY->add_ex( QUERY, "basic/blit2.ck" );
82 QUERY->add_ex( QUERY, "stk/mand-o-matic.ck" );
83 QUERY->add_ex( QUERY, "math/randomize.ck" );
84 QUERY->add_ex( QUERY, "math/maybe.ck" );
85 QUERY->add_ex( QUERY, "math/int-dist.ck" );
86 QUERY->add_ex( QUERY, "math/map.ck" );
87
88 // add abs
89 QUERY->add_sfun( QUERY, abs_impl, "int", "abs" );
90 QUERY->add_arg( QUERY, "int", "value" );
91 QUERY->doc_func( QUERY, "Return absolute value of an integer value." );
92
93 // add fabs
94 QUERY->add_sfun( QUERY, fabs_impl, "float", "fabs" );
95 QUERY->add_arg( QUERY, "float", "value" );
96 QUERY->doc_func( QUERY, "Return absolute value of a floating point value." );
97
98 // add sgn
99 QUERY->add_sfun( QUERY, sgn_impl, "float", "sgn" );
100 QUERY->add_arg( QUERY, "float", "value" );
101 QUERY->doc_func( QUERY, "Return sign of 'value' as -1.0 (negative), 0.0, or 1.0 (positive)." );
102
103 // sin
104 QUERY->add_sfun( QUERY, sin_impl, "float", "sin" );
105 QUERY->add_arg( QUERY, "float", "x" );
106 QUERY->doc_func( QUERY, "Compute sine of x (measured in radians)." );
107
108 // cos
109 QUERY->add_sfun( QUERY, cos_impl, "float", "cos" );
110 QUERY->add_arg( QUERY, "float", "x" );
111 QUERY->doc_func( QUERY, "Compute cosine of x (measured in radians)." );
112
113 // tan
114 QUERY->add_sfun( QUERY, tan_impl, "float", "tan" );
115 QUERY->add_arg( QUERY, "float", "x" );
116 QUERY->doc_func( QUERY, "Compute tangent of x (measured in radians)." );
117
118 // asin
119 QUERY->add_sfun( QUERY, asin_impl, "float", "asin" );
120 QUERY->add_arg( QUERY, "float", "x" );
121 QUERY->doc_func( QUERY, "Compute arcsine of x; result in [-pi/2, +pi/2]." );
122
123 // acos
124 QUERY->add_sfun( QUERY, acos_impl, "float", "acos" );
125 QUERY->add_arg( QUERY, "float", "x" );
126 QUERY->doc_func( QUERY, "Compute arccosine of x; result in [0, pi]." );
127
128 // atan
129 QUERY->add_sfun( QUERY, atan_impl, "float", "atan" );
130 QUERY->add_arg( QUERY, "float", "x" );
131 QUERY->doc_func( QUERY, "Compute arctangent of x; result in [-pi/2, +pi/2]." );
132
133 // atan2
134 QUERY->add_sfun( QUERY, atan2_impl, "float", "atan2" );
135 QUERY->add_arg( QUERY, "float", "y" );
136 QUERY->add_arg( QUERY, "float", "x" );
137 QUERY->doc_func( QUERY, "Compute arc tangent of two variables (y/x)." );
138
139 // sinh
140 QUERY->add_sfun( QUERY, sinh_impl, "float", "sinh" );
141 QUERY->add_arg( QUERY, "float", "x" );
142 QUERY->doc_func( QUERY, "Compute the hyperbolic sine of x." );
143
144 // cosh
145 QUERY->add_sfun( QUERY, cosh_impl, "float", "cosh" );
146 QUERY->add_arg( QUERY, "float", "x" );
147 QUERY->doc_func( QUERY, "Compute the hyperbolic cosine of x." );
148
149 // tanh
150 QUERY->add_sfun( QUERY, tanh_impl, "float", "tanh" );
151 QUERY->add_arg( QUERY, "float", "x" );
152 QUERY->doc_func( QUERY, "Compute the hyperbolic tangent of x." );
153
154 // hypot
155 QUERY->add_sfun( QUERY, hypot_impl, "float", "hypot" );
156 QUERY->add_arg( QUERY, "float", "x" );
157 QUERY->add_arg( QUERY, "float", "y" );
158 QUERY->doc_func( QUERY, "Compute the euclidean distance sqrt(x*x+y*y)." );
159
160 // Fast versions of trig functions!!!
161 // Warning: not always accurate
162
163 // sin
164 QUERY->add_sfun( QUERY, fast_sin_impl, "float", "ssin" );
165 QUERY->add_arg( QUERY, "float", "x" );
166 QUERY->doc_func( QUERY, "Speedy approximation of sine (measured in radians). Uses a Padé approximant, and is recommended to only use inputs between -pi and +pi." );
167
168 // cos
169 QUERY->add_sfun( QUERY, fast_cos_impl, "float", "scos" );
170 QUERY->add_arg( QUERY, "float", "x" );
171 QUERY->doc_func( QUERY, "Speedy approximation of cosine (measured in radians). Uses a Padé approximant, and is recommended to only use inputs between -pi and +pi." );
172
173 // tan
174 QUERY->add_sfun( QUERY, fast_tan_impl, "float", "stan" );
175 QUERY->add_arg( QUERY, "float", "x" );
176 QUERY->doc_func( QUERY, "Speedy approximation of tangent (measured in radians). Uses a Padé approximant, and is recommended to only use inputs between -pi/2 and +pi/2." );
177
178 // sinh
179 QUERY->add_sfun( QUERY, fast_sinh_impl, "float", "ssinh" );
180 QUERY->add_arg( QUERY, "float", "x" );
181 QUERY->doc_func( QUERY, "Speedy approximation of hyperbolic sine. Uses a Padé approximant, and is recommended to only use inputs between -5 and +5." );
182
183 // cosh
184 QUERY->add_sfun( QUERY, fast_cosh_impl, "float", "scosh" );
185 QUERY->add_arg( QUERY, "float", "x" );
186 QUERY->doc_func( QUERY, "Speedy approximation of the hyperbolic cosine. Uses a Padé approximant, and is recommended to only use inputs between -5 and +5." );
187
188 // tanh
189 QUERY->add_sfun( QUERY, fast_tanh_impl, "float", "stanh" );
190 QUERY->add_arg( QUERY, "float", "x" );
191 QUERY->doc_func( QUERY, "Speedy approximation of the hyperbolic tangent. Uses a Padé approximant, and is recommended to only use inputs between -5 and +5." );
192
193 // exp
194 QUERY->add_sfun( QUERY, fast_exp_impl, "float", "sexp" );
195 QUERY->add_arg( QUERY, "float", "x" );
196 QUERY->doc_func( QUERY, "Speedy approximation of e^x, the base-e exponential. Uses a Padé approximant, and is recommended to only use inputs between -6 and +4." );
197
198 // fast inverse-square root
199 QUERY->add_sfun( QUERY, fast_inv_sqrt, "float", "sinsqrt" );
200 QUERY->add_arg( QUERY, "float", "x" );
201 QUERY->doc_func( QUERY, "A speedy (fast) approximation of the inverse square root. Popularized by Quake" );
202
203
204 // End of fast implementations of trig functions
205
206 // pow
207 QUERY->add_sfun( QUERY, pow_impl, "float", "pow" );
208 QUERY->add_arg( QUERY, "float", "x" );
209 QUERY->add_arg( QUERY, "float", "y" );
210 QUERY->doc_func( QUERY, "Compute x raised to the y-th power." );
211
212 // sqrt
213 QUERY->add_sfun( QUERY, sqrt_impl, "float", "sqrt" );
214 QUERY->add_arg( QUERY, "float", "x" );
215 QUERY->doc_func( QUERY, "Compute the non-negative square root of x." );
216
217 // exp
218 QUERY->add_sfun( QUERY, exp_impl, "float", "exp" );
219 QUERY->add_arg( QUERY, "float", "x" );
220 QUERY->doc_func( QUERY, "Compute e^x, the base-e exponential of x." );
221
222 // exp2
223 QUERY->add_sfun( QUERY, exp2_impl, "float", "exp2" );
224 QUERY->add_arg( QUERY, "float", "x" );
225 QUERY->doc_func( QUERY, "Compute 2^x, the base-2 exponential of x." );
226
227 // log
228 QUERY->add_sfun( QUERY, log_impl, "float", "log" );
229 QUERY->add_arg( QUERY, "float", "x" );
230 QUERY->doc_func( QUERY, "Compute the natural logarithm of x." );
231
232 // log2
233 QUERY->add_sfun( QUERY, log2_impl, "float", "log2" );
234 QUERY->add_arg( QUERY, "float", "x" );
235 QUERY->doc_func( QUERY, "Compute the logarithm of x to base 2." );
236
237 // log10
238 QUERY->add_sfun( QUERY, log10_impl, "float", "log10" );
239 QUERY->add_arg( QUERY, "float", "x" );
240 QUERY->doc_func( QUERY, "Compute the logarithm of x to base 10." );
241
242 // floor
243 QUERY->add_sfun( QUERY, floor_impl, "float", "floor" );
244 QUERY->add_arg( QUERY, "float", "x" );
245 QUERY->doc_func( QUERY, "Return the largest integer value (returned as float) not greater than x." );
246
247 // ceil
248 QUERY->add_sfun( QUERY, ceil_impl, "float", "ceil" );
249 QUERY->add_arg( QUERY, "float", "x" );
250 QUERY->doc_func( QUERY, "Return the smallest integer value (returned as float) not less than x." );
251
252 // round
253 QUERY->add_sfun( QUERY, round_impl, "float", "round" );
254 QUERY->add_arg( QUERY, "float", "x" );
255 QUERY->doc_func( QUERY, "Return the integer value (returned as float) nearest to x (rounding halfway cases away from zero)." );
256
257 // trunc
258 QUERY->add_sfun( QUERY, trunc_impl, "float", "trunc" );
259 QUERY->add_arg( QUERY, "float", "x" );
260 QUERY->doc_func( QUERY, "Return the integer value nearest to but no greater in magnitude than x." );
261
262 // fmod
263 QUERY->add_sfun( QUERY, fmod_impl, "float", "fmod" );
264 QUERY->add_arg( QUERY, "float", "x" );
265 QUERY->add_arg( QUERY, "float", "y" );
266 QUERY->doc_func( QUERY, "Compute the floating-point remainder of x / y." );
267
268 // remainder
269 QUERY->add_sfun( QUERY, remainder_impl, "float", "remainder" );
270 QUERY->add_arg( QUERY, "float", "x" );
271 QUERY->add_arg( QUERY, "float", "y" );
272 QUERY->doc_func( QUERY, "Compute the value r such that r=x-n*y, where n is the integer nearest the exact value of x / y. If there are two integers closest to x / y, n shall be the even one. If r is zero, it is given the same sign as x." );
273
274 // min | 1.5.2.2 (ge) added -- NOTE: for now put before float version; overloading needs to implement best match
275 QUERY->add_sfun( QUERY, min_int_impl, "int", "min" );
276 QUERY->add_arg( QUERY, "int", "x" );
277 QUERY->add_arg( QUERY, "int", "y" );
278 QUERY->doc_func( QUERY, "Return the lesser of x and y (int)." );
279
280 // min
281 QUERY->add_sfun( QUERY, min_impl, "float", "min" );
282 QUERY->add_arg( QUERY, "float", "x" );
283 QUERY->add_arg( QUERY, "float", "y" );
284 QUERY->doc_func( QUERY, "Return the lesser of x and y (float)." );
285
286 // max | 1.5.2.2 (ge) added -- NOTE: for now put before float version; overloading needs to implement best match
287 QUERY->add_sfun( QUERY, max_int_impl, "int", "max" );
288 QUERY->add_arg( QUERY, "int", "x" );
289 QUERY->add_arg( QUERY, "int", "y" );
290 QUERY->doc_func( QUERY, "Return the greater of x and y (integer)." );
291
292 // max
293 //! see \example powerup.ck
294 QUERY->add_sfun( QUERY, max_impl, "float", "max" );
295 QUERY->add_arg( QUERY, "float", "x" );
296 QUERY->add_arg( QUERY, "float", "y" );
297 QUERY->doc_func( QUERY, "Return the greater of x and y (float)." );
298
299 // isinf
300 QUERY->add_sfun( QUERY, isinf_impl, "int", "isinf" );
301 QUERY->add_arg( QUERY, "float", "x" );
302 QUERY->doc_func( QUERY, "Return true if x is infinity, else return false.");
303
304 // isnan
305 QUERY->add_sfun( QUERY, isnan_impl, "int", "isnan" );
306 QUERY->add_arg( QUERY, "float", "x" );
307 QUERY->doc_func( QUERY, "Return true if x is not a number, else return false.");
308
309 // equals() for float
310 QUERY->add_sfun( QUERY, equal_impl, "int", "equal" );
311 QUERY->add_arg( QUERY, "float", "lhs" );
312 QUERY->add_arg( QUERY, "float", "rhs" );
313 QUERY->doc_func( QUERY, "Return whether two floats are considered equal." );
314
315 // equals() for float with eqsilon
316 QUERY->add_sfun( QUERY, equale_impl, "int", "equal" );
317 QUERY->add_arg( QUERY, "float", "lhs" );
318 QUERY->add_arg( QUERY, "float", "rhs" );
319 QUERY->add_arg( QUERY, "float", "epsilon" );
320 QUERY->doc_func( QUERY, "Return whether two floats are considered equal, with a third argument to specify the epsilon (the default in Math.equals(float,float) is 1e-8)" );
321
322 // nextpow2
323 QUERY->add_sfun( QUERY, nextpow2_impl, "int", "nextpow2" );
324 QUERY->add_arg( QUERY, "int", "x" );
325 QUERY->doc_func( QUERY, "Compute the smallest power-of-2 greater than x." );
326
327 // ensurepow2
328 QUERY->add_sfun( QUERY, ensurepow2_impl, "int", "ensurePow2" );
329 QUERY->add_arg( QUERY, "int", "x" );
330 QUERY->doc_func( QUERY, "Return the smallest power-of-2 greater than or equal to the value of x.");
331
332 // add mtof
333 QUERY->add_sfun( QUERY, mtof_impl, "float", "mtof" ); //! midi note to frequency
334 QUERY->add_arg( QUERY, "float", "value" );
335 QUERY->doc_func( QUERY, "Convert a MIDI note number to frequency (Hz). Note that the input value is of type float and supports fractional note numbers." );
336
337 // add ftom
338 QUERY->add_sfun( QUERY, ftom_impl, "float", "ftom" ); //! frequency to midi note
339 QUERY->add_arg( QUERY, "float", "value" );
340 QUERY->doc_func( QUERY, "Convert frequency (Hz) to MIDI note number space." );
341
342 // add powtodb
343 QUERY->add_sfun( QUERY, powtodb_impl, "float", "powtodb" ); //! linear power to decibel
344 QUERY->add_arg( QUERY, "float", "value" );
345 QUERY->doc_func( QUERY, "Convert signal power ratio to decibels (dB)." );
346
347 // add rmstodb
348 QUERY->add_sfun( QUERY, rmstodb_impl, "float", "rmstodb" ); //! rms to decibel
349 QUERY->add_arg( QUERY, "float", "value" );
350 QUERY->doc_func( QUERY, "Convert linear amplitude to decibels (dB)." );
351
352 // add dbtopow
353 QUERY->add_sfun( QUERY, dbtopow_impl, "float", "dbtopow" ); //! decibel to linear
354 QUERY->add_arg( QUERY, "float", "value" );
355 QUERY->doc_func( QUERY, "Convert decibels (dB) to signal power ratio." );
356
357 // add dbtorms
358 QUERY->add_sfun( QUERY, dbtorms_impl, "float", "dbtorms" ); //! decibel to rms
359 QUERY->add_arg( QUERY, "float", "value" );
360 QUERY->doc_func( QUERY, "Convert decibles (dB) to linear amplitude." );
361
362 // add re
363 QUERY->add_sfun( QUERY, re_impl, "float", "re" ); //! real component of complex
364 QUERY->add_arg( QUERY, "complex", "v" );
365 QUERY->doc_func( QUERY, "Return the real component of complex value v." );
366
367 // add im
368 QUERY->add_sfun( QUERY, im_impl, "float", "im" ); //! imaginary component of complex
369 QUERY->add_arg( QUERY, "complex", "v" );
370 QUERY->doc_func( QUERY, "Return the imaginary component of complex value v." );
371
372 // add mag
373 QUERY->add_sfun( QUERY, modulus_impl, "float", "mag" ); //! mag
374 QUERY->add_arg( QUERY, "polar", "v" );
375 QUERY->doc_func( QUERY, "Return the magnitude component of polar value v." );
376
377 // add phase
378 QUERY->add_sfun( QUERY, phase_impl, "float", "phase" ); //! phase
379 QUERY->add_arg( QUERY, "polar", "v" );
380 QUERY->doc_func( QUERY, "Return the phase component of polar value v." );
381
382 // add rtop
383 QUERY->add_sfun( QUERY, rtop_impl, "int", "rtop" ); // rect to polar
384 QUERY->add_arg( QUERY, "complex[]", "from" );
385 QUERY->add_arg( QUERY, "polar[]", "to" );
386 QUERY->doc_func( QUERY, "Convert complex values to polar values; returns number of values converted." );
387
388 // add ptor
389 QUERY->add_sfun( QUERY, ptor_impl, "int", "ptor" ); // polar to rect
390 QUERY->add_arg( QUERY, "polar[]", "from" );
391 QUERY->add_arg( QUERY, "complex[]", "to" );
392 QUERY->doc_func( QUERY, "Convert polar values to complex values; returns number of values converted." );
393
394 // add random (1.3.1.0) | [0,(2^31)-1]
395 QUERY->add_sfun( QUERY, random_impl, "int", "random");
396 QUERY->doc_func( QUERY, "Return successive pseudo-random integer numbers in the range [0, Math.RANDOM_MAX]]." );
397
398 // add random2 (1.3.1.0)
399 QUERY->add_sfun( QUERY, random2_impl, "int", "random2" ); //! integer between [min,max]
400 QUERY->add_arg( QUERY, "int", "min" );
401 QUERY->add_arg( QUERY, "int", "max" );
402 QUERY->doc_func( QUERY, "Return successive pseudo-random numbers in the range [min, max]." );
403
404 // add randomf (1.3.1.0) -- NOTE different in return semantics
405 QUERY->add_sfun( QUERY, randomf_impl, "float", "randomf" ); //! random between 0 and 1.0
406 QUERY->doc_func( QUERY, "Return successive pseudo-random floating-point numbers in the range [0,1]." );
407
408 // add random2f (1.3.1.0)
409 QUERY->add_sfun( QUERY, random2f_impl, "float", "random2f" ); //! random between min and max
410 QUERY->add_arg( QUERY, "float", "min" );
411 QUERY->add_arg( QUERY, "float", "max" );
412 QUERY->doc_func( QUERY, "Return successive pseudo-random floating-point numbers in the range [min, max]." );
413
414 // add srandom (1.3.1.0)
415 QUERY->add_sfun( QUERY, srandom_impl, "void", "srandom" );
416 QUERY->add_arg( QUERY, "int", "seed" );
417 QUERY->doc_func( QUERY, "Seed the random number generator (RNG). Different seeds will generate very different sequences of random numbers even if the seeds are close together. Alternatively, a deterministic sequence of pseudo-random numbers can repeatably generated by setting the same seed.");
418
419 // add randomize
420 QUERY->add_sfun( QUERY, randomize_impl, "void", "randomize" );
421 QUERY->doc_func( QUERY, "Randomize the seed of the random number generator (RNG), using an non-deterministic mechanism. Whereas srandom() explicitly seeds the RNG and will produce a deterministic sequence of pseudo-random numbers, randomize() \"shakes things up\" and causes RNG to start generating from a practically unpredicable seed. The quality of randomize() depends on the underlying implementation." );
422
423 // go ahead and seed (the code can seed again for repeatability; 1.3.1.0)
424 // updated to use ck_srandom() wrapper | 1.4.2.0 (ge)
425 // ck_srandom( (unsigned)time( NULL ) );
426 // updated to use ck_randomize() | 1.5.0.4 (ge)
427 ck_randomize();
428
429 // add gauss (ge: added 1.3.5.3)
430 QUERY->add_sfun( QUERY, gauss_impl, "float", "gauss" ); //! Gaussian function
431 QUERY->add_arg( QUERY, "float", "x" );
432 QUERY->add_arg( QUERY, "float", "mean" );
433 QUERY->add_arg( QUERY, "float", "sd" );
434 QUERY->doc_func( QUERY, "Compute gaussian function at x, given mean and SD." );
435
436 // add cossim (ge: added 1.5.0.0)
437 QUERY->add_sfun( QUERY, cossim_impl, "float", "cossim" ); //! cosine similarity
438 QUERY->add_arg( QUERY, "float", "a[]" );
439 QUERY->add_arg( QUERY, "float", "b[]" );
440 QUERY->doc_func( QUERY, "Compute the cosine similarity between arrays a and b." );
441
442 // add cossim (ge: added 1.5.0.0)
443 QUERY->add_sfun( QUERY, cossim3d_impl, "float", "cossim" ); //! cosine similarity
444 QUERY->add_arg( QUERY, "vec3", "a" );
445 QUERY->add_arg( QUERY, "vec3", "b" );
446 QUERY->doc_func( QUERY, "Compute the cosine similarity between 3D vectors a and b." );
447
448 // add cossim (ge: added 1.5.0.0)
449 QUERY->add_sfun( QUERY, cossim4d_impl, "float", "cossim" ); //! cosine similarity
450 QUERY->add_arg( QUERY, "vec4", "a" );
451 QUERY->add_arg( QUERY, "vec4", "b" );
452 QUERY->doc_func( QUERY, "Compute the cosine similarity between 4D vectors a and b." );
453
454 // add euclean (ge: added 1.5.0.0)
455 QUERY->add_sfun( QUERY, euclidean_impl, "float", "euclidean" ); //! euclidean similarity
456 QUERY->add_arg( QUERY, "float", "a[]" );
457 QUERY->add_arg( QUERY, "float", "b[]" );
458 QUERY->doc_func( QUERY, "Compute the euclidean distance between arrays a and b." );
459
460 // add euclean (ge: added 1.5.1.7)
461 QUERY->add_sfun( QUERY, euclidean2d_impl, "float", "euclidean" ); //! euclidean similarity
462 QUERY->add_arg( QUERY, "vec2", "a" );
463 QUERY->add_arg( QUERY, "vec2", "b" );
464 QUERY->doc_func( QUERY, "Compute the euclidean distance between 2D vectors a and b." );
465
466 // add euclean (ge: added 1.5.0.0)
467 QUERY->add_sfun( QUERY, euclidean3d_impl, "float", "euclidean" ); //! euclidean similarity
468 QUERY->add_arg( QUERY, "vec3", "a" );
469 QUERY->add_arg( QUERY, "vec3", "b" );
470 QUERY->doc_func( QUERY, "Compute the euclidean distance between 3D vectors a and b." );
471
472 // add euclean (ge: added 1.5.0.0)
473 QUERY->add_sfun( QUERY, euclidean4d_impl, "float", "euclidean" ); //! euclidean similarity
474 QUERY->add_arg( QUERY, "vec4", "a" );
475 QUERY->add_arg( QUERY, "vec4", "b" );
476 QUERY->doc_func( QUERY, "Compute the euclidean distance between 4D vectors a and b." );
477
478 // add map (ge: added 1.5.0.0)
479 QUERY->add_sfun( QUERY, map_impl, "float", "map" ); //! map
480 QUERY->add_arg( QUERY, "float", "value" );
481 QUERY->add_arg( QUERY, "float", "x1" );
482 QUERY->add_arg( QUERY, "float", "y1" );
483 QUERY->add_arg( QUERY, "float", "x2" );
484 QUERY->add_arg( QUERY, "float", "y2" );
485 QUERY->doc_func( QUERY, "Map 'value' from range [x1,y1] into range [x2,y2]; 'value' can be outside range[x1,y1]. (see also: Math.map2())" );
486
487 // add map (ge: added 1.5.0.0)
488 QUERY->add_sfun( QUERY, map2_impl, "float", "map2" ); //! map
489 QUERY->add_arg( QUERY, "float", "value" );
490 QUERY->add_arg( QUERY, "float", "x1" );
491 QUERY->add_arg( QUERY, "float", "y1" );
492 QUERY->add_arg( QUERY, "float", "x2" );
493 QUERY->add_arg( QUERY, "float", "y2" );
494 QUERY->doc_func( QUERY, "Map 'value' from range [x1,y1] into range [x2,y2]; 'value' will be clamped to [x1,y1] if outside range. (see also: Math.map())" );
495
496 // add remap (ge: added 1.5.0.0)
497 QUERY->add_sfun( QUERY, map2_impl, "float", "remap" ); //! remap
498 QUERY->add_arg( QUERY, "float", "value" );
499 QUERY->add_arg( QUERY, "float", "x1" );
500 QUERY->add_arg( QUERY, "float", "y1" );
501 QUERY->add_arg( QUERY, "float", "x2" );
502 QUERY->add_arg( QUERY, "float", "y2" );
503 QUERY->doc_func( QUERY, "Same as Math.map2()." );
504
505 // add clamp (ge: added 1.5.0.0 -- actually migrated from Std)
506 QUERY->add_sfun( QUERY, clamp_impl, "int", "clampi" ); //! clamp to range (int)
507 QUERY->add_arg( QUERY, "int", "value" );
508 QUERY->add_arg( QUERY, "int", "min" );
509 QUERY->add_arg( QUERY, "int", "max" );
510 QUERY->doc_func( QUERY, "Clamp an integer to range [min,max]." );
511
512 // add clampf (ge: added 1.5.0.0 -- actually migrated from Std)
513 QUERY->add_sfun( QUERY, clampf_impl, "float", "clampf" ); //! clamp to range (float)
514 QUERY->add_arg( QUERY, "float", "value" );
515 QUERY->add_arg( QUERY, "float", "min" );
516 QUERY->add_arg( QUERY, "float", "max" );
517 QUERY->doc_func( QUERY, "Clamp a float to range [min,max]." );
518
519 // pi
520 //! see \example math.ck
521 QUERY->add_svar( QUERY, "float", "PI", TRUE, &g_pi );
522 QUERY->doc_var( QUERY, "An approximation of pi. (Same as global keyword 'pi'.)" );
523
524 // 1.4.1.0 (ge): special disable to avoid conflict (don't forget to reenabled)
525 type_engine_enable_reserved( env, "pi", FALSE );
526 QUERY->add_svar( QUERY, "float", "pi", TRUE, &g_pi );
527 QUERY->doc_var( QUERY, "An approximation of pi. (Same as global keyword 'pi'.)" );
528
529 // twopi
530 QUERY->add_svar( QUERY, "float", "two_pi", TRUE, &g_twopi );
531 QUERY->doc_var( QUERY, "An approximation of 2*pi" );
532 // twopi
533 QUERY->add_svar( QUERY, "float", "TWO_PI", TRUE, &g_twopi );
534 QUERY->doc_var( QUERY, "An approximation of 2*pi" );
535
536 // e
537 QUERY->add_svar( QUERY, "float", "E", TRUE, &g_e );
538 QUERY->doc_var( QUERY, "Euler's number; base of the natural logarithm." );
539 // e
540 QUERY->add_svar( QUERY, "float", "e", TRUE, &g_e );
541 QUERY->doc_var( QUERY, "Euler's number; base of the natural logarithm." );
542
543 // float max
544 //assert( sizeof(t_CKFLOAT) == sizeof(double) );
545 QUERY->add_svar( QUERY, "float", "FLOAT_MAX", TRUE, &g_floatMax );
546 QUERY->doc_var( QUERY, "Largest representable floating-point value." );
547
548 // float min
549 QUERY->add_svar( QUERY, "float", "FLOAT_MIN_MAG", TRUE, &g_floatMin );
550 QUERY->doc_var( QUERY, "Smallest representable non-negative floating-point value." );
551
552 // int max
553 #ifdef _WIN64 // REFACTOR-2017
554 //assert( sizeof(t_CKINT) == sizeof(long long) );
555 #else
556 //assert( sizeof(t_CKINT) == sizeof(long) );
557 #endif
558 QUERY->add_svar( QUERY, "int", "INT_MAX", TRUE, &g_intMax );
559 QUERY->doc_var( QUERY, "Largest representable integer value." );
560
561 // infinity, using function to avoid potential "smart" compiler warning
562 // g_inf = 1.0 / fzero();
563 // 1.5.0.0 (ge) | updated to use limits
564 g_inf = std::numeric_limits<t_CKFLOAT>::infinity();
565 QUERY->add_svar( QUERY, "float", "INFINITY", TRUE, &g_inf );
566 QUERY->doc_var( QUERY, "Like, infinity." );
567
568 // random max
569 QUERY->add_svar( QUERY, "int", "RANDOM_MAX", TRUE, &g_randomMax );
570 QUERY->doc_var( QUERY, "The largest possible value returned by random()." );
571
572 // i
573 QUERY->add_svar( QUERY, "complex", "I", TRUE, &g_i );
574 QUERY->doc_var( QUERY, "The complex number sqrt(-1)." );
575 QUERY->add_svar( QUERY, "complex", "i", TRUE, &g_i );
576 QUERY->doc_var( QUERY, "The complex number sqrt(-1)." );
577
578 // j
579 QUERY->add_svar( QUERY, "complex", "J", TRUE, &g_i );
580 QUERY->doc_var( QUERY, "The complex number sqrt(-1)." );
581 QUERY->add_svar( QUERY, "complex", "j", TRUE, &g_i );
582 QUERY->doc_var( QUERY, "The complex number sqrt(-1)." );
583
584 // spherical harmonics | (added) 1.5.5.8 by everett 2026
585 QUERY->add_sfun( QUERY, sh_impl, "float[]", "sh" );
586 QUERY->add_arg( QUERY, "int", "order" );
587 QUERY->add_arg( QUERY, "float", "azimuth" );
588 QUERY->add_arg( QUERY, "float", "zenith" );
589 QUERY->doc_func(QUERY, "Returns an array of size (N+1)^2, containing ACN ordered spherical harmonics.");
590
591 // add examples
592 // QUERY->add_ex( QUERY, "map.ck" );
593 // QUERY->add_ex( QUERY, "math-help.ck" );
594
595 // done
596 QUERY->end_class( QUERY );
597
598 // 1.4.1.0 (ge): IMPORTANT re-enable keyword
599 type_engine_enable_reserved( env, "pi", TRUE );
600
601 return TRUE;
602 }
603
604 // sin
605 CK_DLL_SFUN( sin_impl )
606 {
607 RETURN->v_float = ::sin( GET_CK_FLOAT(ARGS) );
608 }
609
610 // cos
611 CK_DLL_SFUN( cos_impl )
612 {
613 RETURN->v_float = ::cos( GET_CK_FLOAT(ARGS) );
614 }
615
616 // tan
617 CK_DLL_SFUN( tan_impl )
618 {
619 RETURN->v_float = ::tan( GET_CK_FLOAT(ARGS) );
620 }
621
622 // asin
623 CK_DLL_SFUN( asin_impl )
624 {
625 RETURN->v_float = ::asin( GET_CK_FLOAT(ARGS) );
626 }
627
628 // acos
629 CK_DLL_SFUN( acos_impl )
630 {
631 RETURN->v_float = ::acos( GET_CK_FLOAT(ARGS) );
632 }
633
634 // atan
635 CK_DLL_SFUN( atan_impl )
636 {
637 RETURN->v_float = ::atan( GET_CK_FLOAT(ARGS) );
638 }
639
640 // atan2
641 CK_DLL_SFUN( atan2_impl )
642 {
643 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
644 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1 );
645 RETURN->v_float = ::atan2( x, y );
646 }
647
648 // sinh
649 CK_DLL_SFUN( sinh_impl )
650 {
651 RETURN->v_float = ::sinh( GET_CK_FLOAT(ARGS) );
652 }
653
654 // cosh
655 CK_DLL_SFUN( cosh_impl )
656 {
657 RETURN->v_float = ::cosh( GET_CK_FLOAT(ARGS) );
658 }
659
660 // tanh
661 CK_DLL_SFUN( tanh_impl )
662 {
663 RETURN->v_float = ::tanh( GET_CK_FLOAT(ARGS) );
664 }
665
666 // Fast versions of trig functions!!
667
668 // sin
669 CK_DLL_SFUN( fast_sin_impl )
670 {
671 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
672 t_CKFLOAT x2 = x * x;
673 t_CKFLOAT numerator = -x * (-11511339840 + x2 * (1640635920 + x2 * (-52785432 + x2 * 479249)));
674 t_CKFLOAT denominator = 11511339840 + x2 * (277920720 + x2 * (3177720 + x2 * 18361));
675 RETURN->v_float = numerator / denominator;
676 }
677
678 // cos
679 CK_DLL_SFUN( fast_cos_impl )
680 {
681 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
682 t_CKFLOAT x2 = x * x;
683 t_CKFLOAT numerator = -(-39251520 + x2 * (18471600 + x2 * (-1075032 + 14615 * x2)));
684 t_CKFLOAT denominator = 39251520 + x2 * (1154160 + x2 * (16632 + x2 * 127));
685 RETURN->v_float = numerator / denominator;
686 }
687
688 // tan
689 CK_DLL_SFUN( fast_tan_impl )
690 {
691 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
692 t_CKFLOAT x2 = x * x;
693 t_CKFLOAT numerator = x * (-135135 + x2 * (17325 + x2 * (-378 + x2)));
694 t_CKFLOAT denominator = -135135 + x2 * (62370 + x2 * (-3150 + 28 * x2));
695 RETURN->v_float = numerator / denominator;
696 }
697
698
699 // sinh
700 CK_DLL_SFUN( fast_sinh_impl )
701 {
702 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
703 t_CKFLOAT x2 = x * x;
704 t_CKFLOAT numerator = -x * (11511339840 + x2 * (1640635920 + x2 * (52785432 + x2 * 479249)));
705 t_CKFLOAT denominator = -11511339840 + x2 * (277920720 + x2 * (-3177720 + x2 * 18361));
706 RETURN->v_float = numerator / denominator;
707 }
708
709 // cosh
710 CK_DLL_SFUN( fast_cosh_impl )
711 {
712 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
713 t_CKFLOAT x2 = x * x;
714 t_CKFLOAT numerator = -(39251520 + x2 * (18471600 + x2 * (1075032 + 14615 * x2)));
715 t_CKFLOAT denominator = -39251520 + x2 * (1154160 + x2 * (-16632 + 127 * x2));
716 RETURN->v_float = numerator / denominator;
717 }
718
719 // tanh
720 CK_DLL_SFUN( fast_tanh_impl )
721 {
722 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
723 t_CKFLOAT x2 = x * x;
724 t_CKFLOAT numerator = x * (135135 + x2 * (17325 + x2 * (378 + x2)));
725 t_CKFLOAT denominator = 135135 + x2 * (62370 + x2 * (3150 + 28 * x2));
726 RETURN->v_float = numerator / denominator;
727 }
728
729 // exp
730 CK_DLL_SFUN( fast_exp_impl )
731 {
732 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
733 t_CKFLOAT numerator = 1680 + x * (840 + x * (180 + x * (20 + x)));
734 t_CKFLOAT denominator = 1680 + x *(-840 + x * (180 + x * (-20 + x)));
735 RETURN->v_float = numerator / denominator;
736 }
737
738
739 // fast inverse square root (of quake fame)
740 CK_DLL_SFUN( fast_inv_sqrt )
741 {
742 t_CKFLOAT num = GET_CK_FLOAT(ARGS);
743
744 int i;
745 float x, y;
746 x = num * 0.5;
747 y = num;
748 i = *(int*)&y; // copy bits from y into i so we can bit shift
749
750 i = 0x5f3759df - (i >> 1); // original, has .175% error
751 i = 0x5F375A86 - (i >> 1); // Chris Lomont's updated magic number, .09% error after 1 newton iteration
752
753 y = *(float*)&i; // copy back into float
754
755 // newton iteration
756 y = (y * (1.5 - (x * y * y)));
757 y = (y * (1.5 - (x * y * y))); // optional, improves accuracy
758 RETURN->v_float = num * y;
759 }
760
761
762 // end of fast versions of trig functions
763
764 // hypot
765 CK_DLL_SFUN( hypot_impl )
766 {
767 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
768 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
769 RETURN->v_float = ::hypot( x, y );
770 }
771
772 // pow
773 CK_DLL_SFUN( pow_impl )
774 {
775 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
776 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
777 RETURN->v_float = ::pow( x, y );
778 }
779
780 // sqrt
781 CK_DLL_SFUN( sqrt_impl )
782 {
783 RETURN->v_float = ::sqrt( GET_CK_FLOAT(ARGS) );
784 }
785
786 // exp
787 CK_DLL_SFUN( exp_impl )
788 {
789 RETURN->v_float = ::exp( GET_CK_FLOAT(ARGS) );
790 }
791
792 // exp2
793 CK_DLL_SFUN( exp2_impl )
794 {
795 RETURN->v_float = ::pow( 2, GET_CK_FLOAT(ARGS) );
796 // exp2 is not in some earlier Visual C++ version, e.g., VC++ 2010
797 }
798
799 // log
800 CK_DLL_SFUN( log_impl )
801 {
802 RETURN->v_float = ::log( GET_CK_FLOAT(ARGS) );
803 }
804
805 // log2
806 CK_DLL_SFUN( log2_impl )
807 {
808 RETURN->v_float = ::log( GET_CK_FLOAT(ARGS) )/::log( 2.0 );
809 }
810
811 // log10
812 CK_DLL_SFUN( log10_impl )
813 {
814 RETURN->v_float = ::log10( GET_CK_FLOAT(ARGS) );
815 }
816
817 // floor
818 CK_DLL_SFUN( floor_impl )
819 {
820 RETURN->v_float = ::floor( GET_CK_FLOAT(ARGS) );
821 }
822
823 // ceil
824 CK_DLL_SFUN( ceil_impl )
825 {
826 RETURN->v_float = ::ceil( GET_CK_FLOAT(ARGS) );
827 }
828
829 // round
830 CK_DLL_SFUN( round_impl )
831 {
832 // for older windows version (and miniAudicle windows build)
833 // enable this in the build __CK_MATH_DEFINE_ROUND_TRUNC__
834 RETURN->v_float = round( GET_CK_FLOAT(ARGS) );
835 }
836
837 // trunc
838 CK_DLL_SFUN( trunc_impl )
839 {
840 // for older windows version (and miniAudicle windows build)
841 // enable this in the build __CK_MATH_DEFINE_ROUND_TRUNC__
842 RETURN->v_float = trunc( GET_CK_FLOAT(ARGS) );
843 }
844
845 // fmod
846 CK_DLL_SFUN( fmod_impl )
847 {
848 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
849 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
850 RETURN->v_float = fmod( x, y );
851 }
852
853 // remainder
854 CK_DLL_SFUN( remainder_impl )
855 {
856 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
857 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
858 RETURN->v_float = ck_remainder( x, y );
859 }
860
861 // min
862 CK_DLL_SFUN( min_impl )
863 {
864 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
865 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
866 RETURN->v_float = x < y ? x : y;
867 }
868
869 // max
870 CK_DLL_SFUN( max_impl )
871 {
872 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
873 t_CKFLOAT y = *((t_CKFLOAT *)ARGS + 1);
874 RETURN->v_float = x > y ? x : y;
875 }
876
877 // min
878 CK_DLL_SFUN( min_int_impl )
879 {
880 t_CKINT x = GET_NEXT_INT(ARGS);
881 t_CKINT y = GET_NEXT_INT(ARGS);
882 RETURN->v_int = x < y ? x : y;
883 }
884
885 // max
886 CK_DLL_SFUN( max_int_impl )
887 {
888 t_CKINT x = GET_NEXT_INT(ARGS);
889 t_CKINT y = GET_NEXT_INT(ARGS);
890 RETURN->v_int= x > y ? x : y;
891 }
892
893 // isinf
894 CK_DLL_SFUN( isinf_impl )
895 {
896 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
897 #ifdef __PLATFORM_WINDOWS__
898 RETURN->v_int = !_finite( x );
899 #else
900 RETURN->v_int = isinf( x );
901 #endif
902 }
903
904 // isnan
905 CK_DLL_SFUN( isnan_impl )
906 {
907 t_CKFLOAT x = GET_CK_FLOAT(ARGS);
908 #ifdef __PLATFORM_WINDOWS__
909 RETURN->v_int = _isnan( x );
910 #else
911 RETURN->v_int = isnan( x );
912 #endif
913 }
914
915 // equal( x, y ) -- 1.4.1.1 (added ge)
916 // returns whether x and y (floats) are considered equal
917 CK_DLL_SFUN( equal_impl )
918 {
919 // get arguments
920 t_CKFLOAT x = GET_NEXT_FLOAT(ARGS);
921 t_CKFLOAT y = GET_NEXT_FLOAT(ARGS);
922 // equal
923 RETURN->v_int = ck_equals(x,y);
924 }
925
926 // equal( x, y, e ) -- 1.5.5.6 (added ge)
927 // returns whether x and y (floats) are considered equal, with custom epsilon
928 CK_DLL_SFUN( equale_impl )
929 {
930 // get arguments
931 t_CKFLOAT x = GET_NEXT_FLOAT(ARGS);
932 t_CKFLOAT y = GET_NEXT_FLOAT(ARGS);
933 t_CKFLOAT e = GET_NEXT_FLOAT(ARGS);
934 // equals with epsilon
935 RETURN->v_int = ck_equals_ex(x,y,e);
936 }
937
938 // floatMax
939 CK_DLL_SFUN( floatMax_impl )
940 {
941 // check size
942 if( sizeof(t_CKFLOAT) == 8 )
943 RETURN->v_float = DBL_MAX;
944 else if( sizeof(t_CKFLOAT) == 4 )
945 RETURN->v_float = FLT_MAX;
946 else
947 {
948 // error
949 CK_FPRINTF_STDERR( "[chuck]: internal error determining size of 'float' in floatMax()\n" );
950 RETURN->v_float = 0; // TODO: return NaN?
951 }
952 }
953
954 // intMax
955 CK_DLL_SFUN( intMax_impl )
956 {
957 // check size
958 if( sizeof(t_CKINT) == 4 )
959 RETURN->v_int = LONG_MAX;
960 else
961 {
962 // error
963 CK_FPRINTF_STDERR( "[chuck]: internal error determining size of 'int' in intMax()\n" );
964 RETURN->v_int = 0;
965 }
966 }
967
968 // nextpow2 - thanks to Niklas Werner, via music-dsp
969 CK_DLL_SFUN( nextpow2_impl )
970 {
971 t_CKINT x = GET_CK_INT(ARGS);
972 t_CKINT xx = x;
973 for( ; x &= x-1; xx = x );
974 RETURN->v_int = xx * 2;
975 }
976
977 // ensurepow2 - thanks to Niklas Werner, via music-dsp
978 CK_DLL_SFUN( ensurepow2_impl )
979 {
980 t_CKINT x = GET_CK_INT(ARGS);
981 t_CKINT xx = --x;
982 for( ; x &= x-1; xx = x );
983 RETURN->v_int = xx * 2;
984 }
985
986 // re
987 CK_DLL_SFUN( re_impl )
988 {
989 t_CKCOMPLEX x = GET_NEXT_COMPLEX(ARGS);
990 RETURN->v_float = x.re;
991 }
992
993 // im
994 CK_DLL_SFUN( im_impl )
995 {
996 t_CKCOMPLEX x = GET_NEXT_COMPLEX(ARGS);
997 RETURN->v_float = x.im;
998 }
999
1000 // modulus
1001 CK_DLL_SFUN( modulus_impl )
1002 {
1003 t_CKPOLAR x = GET_NEXT_POLAR(ARGS);
1004 RETURN->v_float = x.modulus;
1005 }
1006
1007 // phase
1008 CK_DLL_SFUN( phase_impl )
1009 {
1010 t_CKPOLAR x = GET_NEXT_POLAR(ARGS);
1011 RETURN->v_float = x.phase;
1012 }
1013
1014
1015 // rtop
1016 CK_DLL_SFUN( rtop_impl )
1017 {
1018 // get array
1019 Chuck_ArrayVec2 * from = (Chuck_ArrayVec2 *)GET_NEXT_OBJECT(ARGS);
1020 Chuck_ArrayVec2 * to = (Chuck_ArrayVec2 *)GET_NEXT_OBJECT(ARGS);
1021
1022 // make sure not null
1023 if( !from || !to )
1024 {
1025 // log
1026 EM_log( CK_LOG_WARNING, "Math.rtop(...) was given one or more NULL arrays..." );
1027 return;
1028 }
1029
1030 // find how much to copy
1031 t_CKUINT count = ck_min( from->size(), to->size() );
1032 t_CKCOMPLEX c;
1033 t_CKCOMPLEX p;
1034
1035 // convert
1036 for( t_CKUINT i = 0; i < count; i++ )
1037 {
1038 // go for it
1039 from->get( i, &c );
1040 p.re = ::hypot( c.re, c.im );
1041 p.im = ::atan2( c.im, c.re );
1042 to->set( i, p );
1043 }
1044
1045 // zero out the rest
1046 // if( count < to->size() ) to->zero( count, to->size() );
1047 if( count < to->size() ) to->set_size( count );
1048
1049 // return number of elements done
1050 RETURN->v_int = count;
1051 }
1052
1053
1054 // ptor
1055 CK_DLL_SFUN( ptor_impl )
1056 {
1057 // get array
1058 Chuck_ArrayVec2 * from = (Chuck_ArrayVec2 *)GET_NEXT_OBJECT(ARGS);
1059 Chuck_ArrayVec2 * to = (Chuck_ArrayVec2 *)GET_NEXT_OBJECT(ARGS);
1060
1061 // make sure not null
1062 if( !from || !to )
1063 {
1064 // log
1065 EM_log( CK_LOG_WARNING, "Math.ptor(...) was given one or more NULL arrays..." );
1066 return;
1067 }
1068
1069 // find how much to copy
1070 t_CKUINT count = ck_min( from->size(), to->size() );
1071 t_CKCOMPLEX c;
1072 t_CKPOLAR p;
1073
1074 // convert
1075 for( t_CKUINT i = 0; i < count; i++ )
1076 {
1077 // go for it
1078 from->get( i, (t_CKCOMPLEX *)&p );
1079 c.re = p.modulus * ::cos( p.phase );
1080 c.im = p.modulus * ::sin( p.phase );
1081 to->set( i, c );
1082 }
1083
1084 // zero out the rest
1085 // if( count < to->capacity() ) to->zero( count, to->size() );
1086 if( count < to->size() ) to->set_size( count );
1087
1088 // return number of elements done
1089 RETURN->v_int = count;
1090 }
1091
1092
1093 // random (added 1.3.1.0)
1094 CK_DLL_SFUN( random_impl )
1095 {
1096 // 1.4.2.0 (ge) | updated to use ck_random() wrapper
1097 RETURN->v_int = ck_random();
1098 }
1099
1100
1101 // randomf (added 1.3.1.0)
1102 CK_DLL_SFUN( randomf_impl )
1103 {
1104 // 1.5.0.4 (ge) | updated to use ck_random_f() wrapper
1105 RETURN->v_float = ck_random_f();
1106 }
1107
1108
1109 // randomf (added 1.3.1.0)
1110 CK_DLL_SFUN( random2f_impl )
1111 {
1112 t_CKFLOAT min = GET_NEXT_FLOAT(ARGS);
1113 t_CKFLOAT max = GET_NEXT_FLOAT(ARGS);
1114 // 1.5.0.4 (ge) | updated to use ck_random_f() wrapper
1115 t_CKFLOAT normRand = ck_random_f();
1116 //CK_FPRINTF_STDERR( "[chuck random2f]: %G --> %G, %G\n", normRand, min, max );
1117 RETURN->v_float = min + (max-min) * normRand;
1118 }
1119
1120
1121 // random2 (added 1.3.1.0)
1122 CK_DLL_SFUN( random2_impl ) // inclusive.
1123 {
1124 // 1.3.1.0: converted int to t_CKINT for 64-bit compatibility
1125 t_CKINT min = *(t_CKINT *)ARGS, max = *((t_CKINT *)ARGS + 1);
1126 t_CKINT range = max - min;
1127 if( range == 0 )
1128 {
1129 RETURN->v_int = min;
1130 }
1131 else
1132 {
1133 if( range > 0 )
1134 {
1135 // 1.4.2.0 (ge) | updated to use ck_random() wrapper
1136 RETURN->v_int = min + (t_CKINT)( (1.0 + range) * ( ck_random()/((t_CKFLOAT)CK_RANDOM_MAX) ) );
1137 }
1138 else
1139 {
1140 // 1.4.2.0 (ge) | updated to use ck_random() wrapper
1141 RETURN->v_int = min - (t_CKINT)( (-range + 1.0) * ( ck_random()/((t_CKFLOAT)CK_RANDOM_MAX) ) );
1142 }
1143 }
1144 }
1145
1146
1147 // srandom (added 1.3.1.0)
1148 CK_DLL_SFUN( srandom_impl )
1149 {
1150 t_CKINT seed = GET_CK_INT(ARGS);
1151 // 1.4.2.0 (ge) | updated to use ck_srandom() wrapper
1152 ck_srandom( (unsigned)seed );
1153 }
1154
1155
1156 // randomize (added 1.5.0.4)
1157 CK_DLL_SFUN( randomize_impl )
1158 {
1159 // undo the seeding with some non-determinism
1160 ck_randomize();
1161 }
1162
1163
1164 // gauss (ge: added 1.3.5.3)
1165 CK_DLL_SFUN( gauss_impl )
1166 {
1167 t_CKFLOAT x = GET_NEXT_FLOAT(ARGS);
1168 t_CKFLOAT mu = GET_NEXT_FLOAT(ARGS);
1169 t_CKFLOAT sd = GET_NEXT_FLOAT(ARGS);
1170
1171 // compute gaussian
1172 RETURN->v_float = (1.0 / (sd*::sqrt(CK_TWO_PI))) * ::exp( -(x-mu)*(x-mu) / (2*sd*sd) );
1173 }
1174
1175
1176 // cossim (ge) | added 1.5.0.0
1177 CK_DLL_SFUN( cossim_impl )
1178 {
1179 Chuck_ArrayFloat * a = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1180 Chuck_ArrayFloat * b = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1181 t_CKINT size = 0;
1182
1183 // in case of error
1184 RETURN->v_float = 0.0;
1185
1186 // if either is NULL, go to error
1187 if( a == NULL || b == NULL )
1188 {
1189 // log
1190 EM_log( CK_LOG_WARNING, "Math.cossim(...) was given one or more NULL arrays..." );
1191 // error out
1192 return;
1193 }
1194
1195 // take either if equal, or the smaller if different
1196 size = a->size() < b->size() ? a->size() : b->size();
1197 // if either is size 0
1198 if( size == 0 )
1199 {
1200 // log
1201 EM_log( CK_LOG_WARNING, "Math.cossim(...) was given one or more 0-length arrays..." );
1202 // error out
1203 return;
1204 }
1205
1206 // get the vecs
1207 std::vector<t_CKFLOAT> & v1 = a->m_vector;
1208 std::vector<t_CKFLOAT> & v2 = b->m_vector;
1209
1210 t_CKFLOAT sum = 0.0, norm1 = 0.0, norm2 = 0.0;
1211 for( t_CKINT i = 0; i < size; i++ )
1212 {
1213 sum += v1[i] * v2[i];
1214 norm1 += v1[i] * v1[i];
1215 norm2 += v2[i] * v2[i];
1216 }
1217
1218 // set return value
1219 RETURN->v_float = sum / (::sqrt(norm1) * ::sqrt(norm2));
1220 }
1221
1222
1223 // cossim (ge) | added 1.5.0.0
1224 CK_DLL_SFUN( cossim3d_impl )
1225 {
1226 t_CKVEC3 a = GET_NEXT_VEC3( ARGS );
1227 t_CKVEC3 b = GET_NEXT_VEC3( ARGS );
1228
1229 // in case of error
1230 RETURN->v_float = 0.0;
1231
1232 t_CKFLOAT sum = 0.0, norm1 = 0.0, norm2 = 0.0;
1233
1234 // x
1235 sum += a.x * b.x;
1236 norm1 += a.x * a.x;
1237 norm2 += b.x * b.x;
1238 // y
1239 sum += a.y * b.y;
1240 norm1 += a.y * a.y;
1241 norm2 += b.y * b.y;
1242 // z
1243 sum += a.z * b.z;
1244 norm1 += a.z * a.z;
1245 norm2 += b.z * b.z;
1246
1247 // if either norm1 or norm2 is 0
1248 if( norm1 == 0 || norm2 == 0 )
1249 {
1250 // log
1251 EM_log( CK_LOG_WARNING, "Math.cossim(...) zero in denominator..." );
1252 // error out
1253 return;
1254 }
1255
1256 // set return value
1257 RETURN->v_float = sum / (::sqrt(norm1) * ::sqrt(norm2));
1258 }
1259
1260
1261 // euclidean4d (ge) | added 1.5.0.0
1262 CK_DLL_SFUN( cossim4d_impl )
1263 {
1264 t_CKVEC4 a = GET_NEXT_VEC4( ARGS );
1265 t_CKVEC4 b = GET_NEXT_VEC4( ARGS );
1266
1267 // in case of error
1268 RETURN->v_float = 0.0;
1269
1270 t_CKFLOAT sum = 0.0, norm1 = 0.0, norm2 = 0.0;
1271
1272 // x
1273 sum += a.x * b.x;
1274 norm1 += a.x * a.x;
1275 norm2 += b.x * b.x;
1276 // y
1277 sum += a.y * b.y;
1278 norm1 += a.y * a.y;
1279 norm2 += b.y * b.y;
1280 // z
1281 sum += a.z * b.z;
1282 norm1 += a.z * a.z;
1283 norm2 += b.z * b.z;
1284 // 2
1285 sum += a.w * b.w;
1286 norm1 += a.w * a.w;
1287 norm2 += b.w * b.w;
1288
1289 // if either norm1 or norm2 is 0
1290 if( norm1 == 0 || norm2 == 0 )
1291 {
1292 // log
1293 EM_log( CK_LOG_WARNING, "Math.cossim(...) zero in denominator..." );
1294 // error out
1295 return;
1296 }
1297
1298 // set return value
1299 RETURN->v_float = sum / (::sqrt(norm1) * ::sqrt(norm2));
1300 }
1301
1302
1303
1304 // euclidean (ge) | added 1.5.0.0
1305 CK_DLL_SFUN( euclidean_impl )
1306 {
1307 Chuck_ArrayFloat * a = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1308 Chuck_ArrayFloat * b = (Chuck_ArrayFloat *)GET_NEXT_OBJECT( ARGS );
1309 t_CKINT size = 0;
1310
1311 // in case of error
1312 RETURN->v_float = 0.0;
1313
1314 // if either is NULL, go to error
1315 if( a == NULL || b == NULL )
1316 {
1317 // log
1318 EM_log( CK_LOG_WARNING, "Math.cossim(...) was given one or more NULL arrays..." );
1319 // error out
1320 return;
1321 }
1322
1323 // take either if equal, or the smaller if different
1324 size = a->size() < b->size() ? a->size() : b->size();
1325 // if either is size 0
1326 if( size == 0 )
1327 {
1328 // log
1329 EM_log( CK_LOG_WARNING, "Math.cossim(...) was given one or more 0-length arrays..." );
1330 // error out
1331 return;
1332 }
1333
1334 // get the vecs
1335 std::vector<t_CKFLOAT> & v1 = a->m_vector;
1336 std::vector<t_CKFLOAT> & v2 = b->m_vector;
1337
1338 t_CKFLOAT d;
1339 t_CKFLOAT sum = 0.0;
1340 for( t_CKINT i = 0; i < size; i++ )
1341 {
1342 d = v1[i] - v2[i];
1343 sum += d*d;
1344 }
1345
1346 // set return value
1347 RETURN->v_float = ::sqrt(sum);
1348 }
1349
1350
1351 // euclidean (ge) | added 1.5.1.7
1352 CK_DLL_SFUN( euclidean2d_impl )
1353 {
1354 t_CKVEC2 a = GET_NEXT_VEC2( ARGS );
1355 t_CKVEC2 b = GET_NEXT_VEC2( ARGS );
1356
1357 // in case of error
1358 RETURN->v_float = 0.0;
1359
1360 t_CKFLOAT d;
1361 t_CKFLOAT sum = 0.0;
1362 d = a.x - b.x; sum += d*d;
1363 d = a.y - b.y; sum += d*d;
1364
1365 // set return value
1366 RETURN->v_float = ::sqrt(sum);
1367 }
1368
1369
1370 // euclidean (ge) | added 1.5.0.0
1371 CK_DLL_SFUN( euclidean3d_impl )
1372 {
1373 t_CKVEC3 a = GET_NEXT_VEC3( ARGS );
1374 t_CKVEC3 b = GET_NEXT_VEC3( ARGS );
1375
1376 // in case of error
1377 RETURN->v_float = 0.0;
1378
1379 t_CKFLOAT d;
1380 t_CKFLOAT sum = 0.0;
1381 d = a.x - b.x; sum += d*d;
1382 d = a.y - b.y; sum += d*d;
1383 d = a.z - b.z; sum += d*d;
1384
1385 // set return value
1386 RETURN->v_float = ::sqrt(sum);
1387 }
1388
1389
1390 // euclidean (ge) | added 1.5.0.0
1391 CK_DLL_SFUN( euclidean4d_impl )
1392 {
1393 t_CKVEC4 a = GET_NEXT_VEC4( ARGS );
1394 t_CKVEC4 b = GET_NEXT_VEC4( ARGS );
1395
1396 // in case of error
1397 RETURN->v_float = 0.0;
1398
1399 t_CKFLOAT d;
1400 t_CKFLOAT sum = 0.0;
1401 d = a.x - b.x; sum += d*d;
1402 d = a.y - b.y; sum += d*d;
1403 d = a.z - b.z; sum += d*d;
1404 d = a.w - b.w; sum += d*d;
1405
1406 // set return value
1407 RETURN->v_float = ::sqrt(sum);
1408 }
1409
1410 // map (ge) | added 1.5.0.0
1411 CK_DLL_SFUN( map_impl )
1412 {
1413 t_CKFLOAT v = GET_NEXT_FLOAT( ARGS );
1414 t_CKFLOAT x1 = GET_NEXT_FLOAT( ARGS );
1415 t_CKFLOAT y1 = GET_NEXT_FLOAT( ARGS );
1416 t_CKFLOAT x2 = GET_NEXT_FLOAT( ARGS );
1417 t_CKFLOAT y2 = GET_NEXT_FLOAT( ARGS );
1418
1419 // t_CKFLOAT min1 = x1 < y1 ? x1 : y1;
1420 // t_CKFLOAT max1 = x1 > y1 ? x1 : y1;
1421
1422 // std::cerr << v << " " << x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;
1423 // remap
1424 RETURN->v_float = x2 + (v-x1)/(y1-x1)*(y2-x2);
1425 }
1426
1427 // map2 (ge) | added 1.5.0.0
1428 CK_DLL_SFUN( map2_impl )
1429 {
1430 t_CKFLOAT v = GET_NEXT_FLOAT( ARGS );
1431 t_CKFLOAT x1 = GET_NEXT_FLOAT( ARGS );
1432 t_CKFLOAT y1 = GET_NEXT_FLOAT( ARGS );
1433 t_CKFLOAT x2 = GET_NEXT_FLOAT( ARGS );
1434 t_CKFLOAT y2 = GET_NEXT_FLOAT( ARGS );
1435
1436 t_CKFLOAT min1 = x1 < y1 ? x1 : y1;
1437 t_CKFLOAT max1 = x1 > y1 ? x1 : y1;
1438
1439 // clamp v in [x1, y1]
1440 if( v < min1 ) v = min1;
1441 else if( v > max1 ) v = max1;
1442
1443 // std::cerr << v << " " << x1 << " " << y1 << " " << x2 << " " << y2 << std::endl;
1444 // remap
1445 RETURN->v_float = x2 + (v - x1) / (y1 - x1) * (y2 - x2);
1446 }
1447
1448 // spherical harmonics implementation | (added) 1.5.5.8 by everett 2026
1449 CK_DLL_SFUN( sh_impl )
1450 {
1451 t_CKINT order = GET_NEXT_INT(ARGS);
1452 t_CKFLOAT direction = GET_NEXT_FLOAT(ARGS);
1453 t_CKFLOAT elevation = GET_NEXT_FLOAT(ARGS);
1454 t_CKFLOAT * coord = new t_CKFLOAT[(order + 1) * (order + 1) + 1];
1455 if( order <= 5 )
1456 {
1457 t_CKUINT size = (order + 1) * (order + 1);
1458 // moved memory ownership to ck_SH emc 2/26 (memory was previously allocated by ck_SH() and deleted by sh_impl)
1459 ck_SH( coord,order, direction, elevation, 0 );
1460 // Create a float[] array
1461 Chuck_DL_Api::Object returnarray = API->object->create( SHRED, API->type->lookup(VM, "float[]"), false );
1462 Chuck_ArrayFloat * coordinatearray = (Chuck_ArrayFloat *)returnarray;
1463 for( t_CKINT i = 0; i < size; i++ )
1464 {
1465 API->object->array_float_push_back(coordinatearray, coord[i]);
1466 }
1467 // set return value
1468 RETURN->v_object = coordinatearray;
1469 }
1470 else
1471 {
1472 // throw exception
1473 API->vm->throw_exception("Math.sh() : InvalidSHOrder", "only up to 5th order currently supported", nullptr);
1474 RETURN->v_int = 0;
1475 }
1476
1477 // clean up
1478 CK_SAFE_DELETE_ARRAY( coord );
1479 }