Revision 40ad675e
Added by Pierre-Loïc Garoche almost 6 years ago
include/simulink_math_fcn.c | ||
---|---|---|
1 |
#include "simulink_math_fcn.h" |
|
2 |
#include <math.h> |
|
3 |
|
|
4 |
/* function exp_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
5 |
double exp_scalar_real (double x) { |
|
6 |
return exp(x); |
|
7 |
} |
|
8 |
|
|
9 |
|
|
10 |
/* function log_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
11 |
double log_scalar_real (double x) { |
|
12 |
return log(x); |
|
13 |
} |
|
14 |
/* function _10u_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
15 |
double _10u_scalar_real (double x) { |
|
16 |
return pow(10.,x); |
|
17 |
} |
|
18 |
|
|
19 |
/* function log10_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
20 |
double log10_scalar_real (double x) { |
|
21 |
return log10(x); |
|
22 |
} |
|
23 |
|
|
24 |
/* function magnitude_2_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
25 |
double magnitude_2_scalar_real (double x) { |
|
26 |
return pow(fabs(x), 2.); |
|
27 |
} |
|
28 |
|
|
29 |
/* function square_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
30 |
double square_scalar_real (double x) { |
|
31 |
return pow(x, 2.); |
|
32 |
} |
|
33 |
|
|
34 |
/* function pow_scalar_real (x,y: real) returns (z: real) prototype C lib m; */ |
|
35 |
double pow_scalar_real (double x, double y) { |
|
36 |
return pow(x, y); |
|
37 |
} |
|
38 |
|
|
39 |
/* function conj_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
40 |
double conj_scalar_real (double x) { |
|
41 |
return x; // identity for real |
|
42 |
} |
|
43 |
|
|
44 |
/* function reciprocal_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
45 |
double reciprocal_scalar_real (double x) { |
|
46 |
return 1./x; |
|
47 |
} |
|
48 |
|
|
49 |
/* function hypot_scalar_real (x,y: real) returns (z: real) prototype C lib m; */ |
|
50 |
double hypot_scalar_real (double x, double y) { |
|
51 |
return sqrt(x*x + y*y); |
|
52 |
} |
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
/* |
|
57 |
mod function produces a result that is either zero or has the same sign as the divisor. |
|
58 |
rem function produces a result that is either zero or has the same sign as the dividend. |
|
59 |
mod(a,0) returns a |
|
60 |
rem(a,0) returns NaN. |
|
61 |
|
|
62 |
function rem_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m; |
|
63 |
function rem_scalar_real_double_double_double (x,y: double) returns (z: double) prototype C lib m; |
|
64 |
function rem_scalar_real_double_int_double (x: double; y: int) returns (z: double) prototype C lib m; |
|
65 |
function rem_scalar_real_int_double_double (x: int; y: double) returns (z: double) prototype C lib m; |
|
66 |
|
|
67 |
function mod_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m; |
|
68 |
function mod_scalar_real_double_double_double (x,y: double) returns (z: double) prototype C lib m; |
|
69 |
function mod_scalar_real_double_int_double (x: double; y: int) returns (z: double) prototype C lib m; |
|
70 |
function mod_scalar_real_int_double_double (x: int; y: double) returns (z: double) prototype C lib m; |
|
71 |
*/ |
|
72 |
|
|
73 |
int rem_scalar_real_int_int_int (int x, int y) { |
|
74 |
return x%y; |
|
75 |
} |
|
76 |
|
|
77 |
int mod_scalar_real_int_int_int (int x, int y) { |
|
78 |
int tmp; |
|
79 |
if (y == 0) { return x; }; |
|
80 |
tmp = x%y; |
|
81 |
if (y < 0 && tmp > 0) { |
|
82 |
return tmp+y; |
|
83 |
} |
|
84 |
else { |
|
85 |
return tmp; |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
double rem_scalar_real_double_double_double (double x, double y) { |
|
90 |
return fmod(x, y); |
|
91 |
} |
|
92 |
|
|
93 |
double mod_scalar_real_double_double_double (double x, double y) { |
|
94 |
double tmp = 0.; |
|
95 |
if (y == 0.) { return x; }; |
|
96 |
tmp = fmod(x, y); |
|
97 |
if (y < 0. && tmp > 0.) { |
|
98 |
return tmp+y; |
|
99 |
} |
|
100 |
else { |
|
101 |
return tmp; |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
double rem_scalar_real_double_int_double (double x, int y) { |
|
106 |
return rem_scalar_real_double_double_double (x, (double)y); |
|
107 |
} |
|
108 |
|
|
109 |
double rem_scalar_real_int_double_double (int x, double y) { |
|
110 |
return rem_scalar_real_double_double_double ((double)x, y); |
|
111 |
} |
|
112 |
|
|
113 |
|
|
114 |
double mod_scalar_real_double_int_double (double x, int y) { |
|
115 |
return (mod_scalar_real_double_double_double (x, (double)y)); |
|
116 |
} |
|
117 |
|
|
118 |
double mod_scalar_real_int_double_double (int x, double y) { |
|
119 |
return (mod_scalar_real_double_double_double ((double)x, y)); |
|
120 |
} |
|
121 |
|
|
122 |
/* function transpose_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
|
123 |
|
|
124 |
/* function hermitian_scalar_real (x: real) returns (y: real) prototype C lib m; */ |
include/simulink_math_fcn.lusi | ||
---|---|---|
1 |
(* |
|
2 |
Mathematical functions in Simulink Math Function blocks |
|
3 |
|
|
4 |
All these functions can be applied to scalar value. All but transpose and |
|
5 |
hermitian can be also applied as element-wise operations on vector, matrices |
|
6 |
inputs. transpose and hermitian are applied on vector and matrices as regular |
|
7 |
(non element-wise) operations. |
|
8 |
|
|
9 |
The Lustre library provides only scalar functions for all cases, and, in the future, |
|
10 |
the matrix versions of them. |
|
11 |
|
|
12 |
exp: |
|
13 |
log |
|
14 |
10^u |
|
15 |
log10 |
|
16 |
magnitude^2 |
|
17 |
square |
|
18 |
pow |
|
19 |
conj |
|
20 |
reciprocal |
|
21 |
hypot |
|
22 |
rem |
|
23 |
mod |
|
24 |
transpose |
|
25 |
hermitian |
|
26 |
|
|
27 |
For the moment, we focus only on theoretical types: real, complex. |
|
28 |
A future version can be specialized for concrete datatypes (single, double, |
|
29 |
(u)intXX). |
|
30 |
|
|
31 |
*) |
|
32 |
|
|
33 |
-- open <math> |
|
34 |
function exp_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
35 |
function log_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
36 |
function _10u_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
37 |
function log10_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
38 |
|
|
39 |
-- complex modulus: |x|^2 |
|
40 |
function magnitude_2_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
41 |
function square_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
42 |
function pow_scalar_real (x,y: real) returns (z: real) prototype C lib m; |
|
43 |
function conj_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
44 |
function reciprocal_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
45 |
function hypot_scalar_real (x,y: real) returns (z: real) prototype C lib m; |
|
46 |
|
|
47 |
function rem_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m; |
|
48 |
function rem_scalar_real_double_double_double (x,y: real) returns (z: real) prototype C lib m; |
|
49 |
function rem_scalar_real_double_int_double (x: real; y: int) returns (z: real) prototype C lib m; |
|
50 |
function rem_scalar_real_int_double_double (x: int; y: real) returns (z: real) prototype C lib m; |
|
51 |
|
|
52 |
function mod_scalar_real_int_int_int (x,y: int) returns (z: int) prototype C lib m; |
|
53 |
function mod_scalar_real_double_double_double (x,y: real) returns (z: real) prototype C lib m; |
|
54 |
function mod_scalar_real_double_int_double (x: real; y: int) returns (z: real) prototype C lib m; |
|
55 |
function mod_scalar_real_int_double_double (x: int; y: real) returns (z: real) prototype C lib m; |
|
56 |
|
|
57 |
(* |
|
58 |
-- function transpose_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
59 |
-- function hermitian_scalar_real (x: real) returns (y: real) prototype C lib m; |
|
60 |
-- function exp_matrix_real (const i,j: int; x: real^i^j) returns (y: real^i^j) prototype C lib m; |
|
61 |
*) |
Also available in: Unified diff
A math library for some functions used in Simulink