math_expressions.hpp (18326B)
1 /** @addtogroup base 2 * @{ 3 */ 4 /* 5 Copyright (C) 2016-2023 Dan Cazarin (https://www.kfrlib.com) 6 This file is part of KFR 7 8 KFR is free software: you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation, either version 2 of the License, or 11 (at your option) any later version. 12 13 KFR is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with KFR. 20 21 If GPL is not suitable for your project, you must purchase a commercial license to use KFR. 22 Buying a commercial license is mandatory as soon as you develop commercial activities without 23 disclosing the source code of your own applications. 24 See https://www.kfrlib.com for details. 25 */ 26 #pragma once 27 28 #include "../math.hpp" 29 #include "basic_expressions.hpp" 30 #include "expression.hpp" 31 32 namespace kfr 33 { 34 35 /** 36 * @brief Returns the trigonometric sine of x. Accepts and returns expressions. 37 */ 38 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 39 KFR_FUNCTION expression_make_function<fn::sin, E1> sin(E1&& x) 40 { 41 return { fn::sin(), std::forward<E1>(x) }; 42 } 43 44 /** 45 * @brief Returns the trigonometric cosine of x. Accepts and returns expressions. 46 */ 47 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 48 KFR_FUNCTION expression_make_function<fn::cos, E1> cos(E1&& x) 49 { 50 return { fn::cos(), std::forward<E1>(x) }; 51 } 52 53 /** 54 * @brief Returns an approximation of the trigonometric sine of x. Accepts and returns expressions. 55 */ 56 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 57 KFR_FUNCTION expression_make_function<fn::fastsin, E1> fastsin(E1&& x) 58 { 59 return { fn::fastsin(), std::forward<E1>(x) }; 60 } 61 62 /** 63 * @brief Returns an approximation of the trigonometric cosine of x. Accepts and returns expressions. 64 */ 65 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 66 KFR_FUNCTION expression_make_function<fn::fastcos, E1> fastcos(E1&& x) 67 { 68 return { fn::fastcos(), std::forward<E1>(x) }; 69 } 70 71 /** 72 * @brief Returns the trigonometric sine of the even elements of the x and 73 * cosine of the odd elements. x must be a vector. Accepts and returns expressions. 74 */ 75 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 76 KFR_FUNCTION expression_make_function<fn::sincos, E1> sincos(E1&& x) 77 { 78 return { fn::sincos(), std::forward<E1>(x) }; 79 } 80 81 /** 82 * @brief Returns the trigonometric cosine of the even elements of the x and 83 * sine of the odd elements. x must be a vector. Accepts and returns expressions. 84 */ 85 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 86 KFR_FUNCTION expression_make_function<fn::cossin, E1> cossin(E1&& x) 87 { 88 return { fn::cossin(), std::forward<E1>(x) }; 89 } 90 91 /** 92 * @brief Returns the trigonometric sine of the x (expressed in degrees). Accepts and returns expressions. 93 */ 94 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 95 KFR_FUNCTION expression_make_function<fn::sindeg, E1> sindeg(E1&& x) 96 { 97 return { fn::sindeg(), std::forward<E1>(x) }; 98 } 99 100 /** 101 * @brief Returns the trigonometric cosine of the x (expressed in degrees). Accepts and returns expressions. 102 */ 103 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 104 KFR_FUNCTION expression_make_function<fn::cosdeg, E1> cosdeg(E1&& x) 105 { 106 return { fn::cosdeg(), std::forward<E1>(x) }; 107 } 108 109 /** 110 * @brief Returns an approximation of the trigonometric sine of the x 111 * (expressed in degrees). Accepts and returns expressions. 112 */ 113 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 114 KFR_FUNCTION expression_make_function<fn::fastsindeg, E1> fastsindeg(E1&& x) 115 { 116 return { fn::fastsindeg(), std::forward<E1>(x) }; 117 } 118 119 /** 120 * @brief Returns an approximation of the trigonometric cosine of the x 121 * (expressed in degrees). Accepts and returns expressions. 122 */ 123 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 124 KFR_FUNCTION expression_make_function<fn::fastcosdeg, E1> fastcosdeg(E1&& x) 125 { 126 return { fn::fastcosdeg(), std::forward<E1>(x) }; 127 } 128 129 /** 130 * @brief Returns the trigonometric sine of the even elements of the x and 131 * cosine of the odd elements. x must be expressed in degrees. Accepts and returns expressions. 132 */ 133 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 134 KFR_FUNCTION expression_make_function<fn::sincosdeg, E1> sincosdeg(E1&& x) 135 { 136 return { fn::sincosdeg(), std::forward<E1>(x) }; 137 } 138 139 /** 140 * @brief Returns the trigonometric cosine of the even elements of the x and 141 * sine of the odd elements. x must be expressed in degrees. Accepts and returns expressions. 142 */ 143 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 144 KFR_FUNCTION expression_make_function<fn::cossindeg, E1> cossindeg(E1&& x) 145 { 146 return { fn::cossindeg(), std::forward<E1>(x) }; 147 } 148 149 /** 150 * @brief Returns the sinc function of x. Accepts and returns expressions. 151 */ 152 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 153 KFR_FUNCTION expression_make_function<fn::sinc, E1> sinc(E1&& x) 154 { 155 return { fn::sinc(), std::forward<E1>(x) }; 156 } 157 158 /// @brief Creates expression that returns the approximate gamma function of an argument 159 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 160 KFR_FUNCTION expression_make_function<fn::gamma, E1> gamma(E1&& x) 161 { 162 return { fn::gamma(), std::forward<E1>(x) }; 163 } 164 165 /// @brief Creates expression that returns the approximate factorial of an argument 166 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 167 KFR_FUNCTION expression_make_function<fn::factorial_approx, E1> factorial_approx(E1&& x) 168 { 169 return { fn::factorial_approx(), std::forward<E1>(x) }; 170 } 171 172 /** 173 * @brief Returns template expression that returns the positive square root of the x. \f$\sqrt{x}\f$ 174 */ 175 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 176 KFR_FUNCTION expression_make_function<fn::sqrt, E1> sqrt(E1&& x) 177 { 178 return { fn::sqrt(), std::forward<E1>(x) }; 179 } 180 181 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 182 KFR_FUNCTION expression_make_function<fn::tan, E1> tan(E1&& x) 183 { 184 return { fn::tan(), std::forward<E1>(x) }; 185 } 186 187 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 188 KFR_FUNCTION expression_make_function<fn::tandeg, E1> tandeg(E1&& x) 189 { 190 return { fn::tandeg(), std::forward<E1>(x) }; 191 } 192 193 /** 194 * @brief Returns template expression that returns the arc sine of x. 195 */ 196 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 197 KFR_INTRINSIC expression_make_function<fn::asin, E1> asin(E1&& x) 198 { 199 return { fn::asin(), std::forward<E1>(x) }; 200 } 201 202 /** 203 * @brief Returns template expression that returns the arc cosine of x. 204 */ 205 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 206 KFR_INTRINSIC expression_make_function<fn::acos, E1> acos(E1&& x) 207 { 208 return { fn::acos(), std::forward<E1>(x) }; 209 } 210 211 /// @brief Returns template expression that returns the sine of the the complex value x 212 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 213 KFR_FUNCTION expression_make_function<fn::csin, E1> csin(E1&& x) 214 { 215 return { fn::csin(), std::forward<E1>(x) }; 216 } 217 218 /// @brief Returns template expression that returns the hyperbolic sine of the complex number x 219 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 220 KFR_FUNCTION expression_make_function<fn::csinh, E1> csinh(E1&& x) 221 { 222 return { fn::csinh(), std::forward<E1>(x) }; 223 } 224 225 /// @brief Returns template expression that returns the cosine of the the complex value x 226 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 227 KFR_FUNCTION expression_make_function<fn::ccos, E1> ccos(E1&& x) 228 { 229 return { fn::ccos(), std::forward<E1>(x) }; 230 } 231 232 /// @brief Returns template expression that returns the hyperbolic cosine of the the complex value x 233 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 234 KFR_FUNCTION expression_make_function<fn::ccosh, E1> ccosh(E1&& x) 235 { 236 return { fn::ccosh(), std::forward<E1>(x) }; 237 } 238 239 /// @brief Returns template expression that returns the squared absolute value (magnitude squared) of the 240 /// complex number x 241 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 242 KFR_FUNCTION expression_make_function<fn::cabssqr, E1> cabssqr(E1&& x) 243 { 244 return { fn::cabssqr(), std::forward<E1>(x) }; 245 } 246 247 /// @brief Returns template expression that returns the absolute value (magnitude) of the complex number x 248 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 249 KFR_FUNCTION expression_make_function<fn::cabs, E1> cabs(E1&& x) 250 { 251 return { fn::cabs(), std::forward<E1>(x) }; 252 } 253 254 /// @brief Returns template expression that returns the phase angle (argument) of the complex number x 255 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 256 KFR_FUNCTION expression_make_function<fn::carg, E1> carg(E1&& x) 257 { 258 return { fn::carg(), std::forward<E1>(x) }; 259 } 260 261 /// @brief Returns template expression that returns the natural logarithm of the complex number x 262 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 263 KFR_FUNCTION expression_make_function<fn::clog, E1> clog(E1&& x) 264 { 265 return { fn::clog(), std::forward<E1>(x) }; 266 } 267 268 /// @brief Returns template expression that returns the binary (base-2) logarithm of the complex number x 269 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 270 KFR_FUNCTION expression_make_function<fn::clog2, E1> clog2(E1&& x) 271 { 272 return { fn::clog2(), std::forward<E1>(x) }; 273 } 274 275 /// @brief Returns template expression that returns the common (base-10) logarithm of the complex number x 276 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 277 KFR_FUNCTION expression_make_function<fn::clog10, E1> clog10(E1&& x) 278 { 279 return { fn::clog10(), std::forward<E1>(x) }; 280 } 281 282 /// @brief Returns template expression that returns \f$e\f$ raised to the complex number x 283 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 284 KFR_FUNCTION expression_make_function<fn::cexp, E1> cexp(E1&& x) 285 { 286 return { fn::cexp(), std::forward<E1>(x) }; 287 } 288 289 /// @brief Returns template expression that returns 2 raised to the complex number x 290 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 291 KFR_FUNCTION expression_make_function<fn::cexp2, E1> cexp2(E1&& x) 292 { 293 return { fn::cexp2(), std::forward<E1>(x) }; 294 } 295 296 /// @brief Returns template expression that returns 10 raised to the complex number x 297 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 298 KFR_FUNCTION expression_make_function<fn::cexp10, E1> cexp10(E1&& x) 299 { 300 return { fn::cexp10(), std::forward<E1>(x) }; 301 } 302 303 /// @brief Returns template expression that converts complex number to polar 304 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 305 KFR_FUNCTION expression_make_function<fn::polar, E1> polar(E1&& x) 306 { 307 return { fn::polar(), std::forward<E1>(x) }; 308 } 309 310 /// @brief Returns template expression that converts complex number to cartesian 311 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 312 KFR_FUNCTION expression_make_function<fn::cartesian, E1> cartesian(E1&& x) 313 { 314 return { fn::cartesian(), std::forward<E1>(x) }; 315 } 316 317 /// @brief Returns template expression that returns square root of the complex number x 318 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 319 KFR_FUNCTION expression_make_function<fn::csqrt, E1> csqrt(E1&& x) 320 { 321 return { fn::csqrt(), std::forward<E1>(x) }; 322 } 323 324 /// @brief Returns template expression that returns square of the complex number x 325 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 326 KFR_FUNCTION expression_make_function<fn::csqr, E1> csqr(E1&& x) 327 { 328 return { fn::csqr(), std::forward<E1>(x) }; 329 } 330 331 /** 332 * @brief Returns template expression that returns the arc tangent of x. 333 */ 334 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 335 KFR_FUNCTION expression_make_function<fn::atan, E1> atan(E1&& x) 336 { 337 return { fn::atan(), std::forward<E1>(x) }; 338 } 339 340 /** 341 * @brief Returns template expression that returns the arc tangent of the x, expressed in degrees. 342 */ 343 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 344 KFR_FUNCTION expression_make_function<fn::atandeg, E1> atandeg(E1&& x) 345 { 346 return { fn::atandeg(), std::forward<E1>(x) }; 347 } 348 349 /** 350 * @brief Returns template expression that returns the arc tangent of y/x. 351 */ 352 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 353 KFR_FUNCTION expression_make_function<fn::atan2, E1, E2> atan2(E1&& x, E2&& y) 354 { 355 return { fn::atan2(), std::forward<E1>(x), std::forward<E2>(y) }; 356 } 357 358 /** 359 * @brief Returns template expression that returns the arc tangent of y/x (expressed in degrees). 360 */ 361 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 362 KFR_FUNCTION expression_make_function<fn::atan2deg, E1, E2> atan2deg(E1&& x, E2&& y) 363 { 364 return { fn::atan2deg(), std::forward<E1>(x), std::forward<E2>(y) }; 365 } 366 367 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 368 KFR_FUNCTION expression_make_function<fn::modzerobessel, E1> modzerobessel(E1&& x) 369 { 370 return { fn::modzerobessel(), std::forward<E1>(x) }; 371 } 372 373 /// @brief Returns template expression that returns the hyperbolic sine of the x 374 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 375 KFR_FUNCTION expression_make_function<fn::sinh, E1> sinh(E1&& x) 376 { 377 return { fn::sinh(), std::forward<E1>(x) }; 378 } 379 380 /// @brief Returns template expression that returns the hyperbolic cosine of the x 381 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 382 KFR_FUNCTION expression_make_function<fn::cosh, E1> cosh(E1&& x) 383 { 384 return { fn::cosh(), std::forward<E1>(x) }; 385 } 386 387 /// @brief Returns template expression that returns the hyperbolic tangent of the x 388 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 389 KFR_FUNCTION expression_make_function<fn::tanh, E1> tanh(E1&& x) 390 { 391 return { fn::tanh(), std::forward<E1>(x) }; 392 } 393 394 /// @brief Returns template expression that returns the hyperbolic cotangent of the x 395 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 396 KFR_FUNCTION expression_make_function<fn::coth, E1> coth(E1&& x) 397 { 398 return { fn::coth(), std::forward<E1>(x) }; 399 } 400 401 /// @brief Returns template expression that returns the hyperbolic sine of the even elements of the x and the 402 /// hyperbolic cosine of the odd elements of the x 403 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 404 KFR_FUNCTION expression_make_function<fn::sinhcosh, E1> sinhcosh(E1&& x) 405 { 406 return { fn::sinhcosh(), std::forward<E1>(x) }; 407 } 408 409 /// @brief Returns template expression that returns the hyperbolic cosine of the even elements of the x and 410 /// the hyperbolic sine of the odd elements of the x 411 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 412 KFR_FUNCTION expression_make_function<fn::coshsinh, E1> coshsinh(E1&& x) 413 { 414 return { fn::coshsinh(), std::forward<E1>(x) }; 415 } 416 417 /// @brief Returns e raised to the given power x. Accepts and returns expressions. 418 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 419 KFR_FUNCTION expression_make_function<fn::exp, E1> exp(E1&& x) 420 { 421 return { fn::exp(), std::forward<E1>(x) }; 422 } 423 424 /// @brief Returns 2 raised to the given power x. Accepts and returns expressions. 425 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 426 KFR_FUNCTION expression_make_function<fn::exp2, E1> exp2(E1&& x) 427 { 428 return { fn::exp2(), std::forward<E1>(x) }; 429 } 430 431 /// @brief Returns 10 raised to the given power x. Accepts and returns expressions. 432 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 433 KFR_FUNCTION expression_make_function<fn::exp10, E1> exp10(E1&& x) 434 { 435 return { fn::exp10(), std::forward<E1>(x) }; 436 } 437 438 /// @brief Returns the natural logarithm of the x. Accepts and returns expressions. 439 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 440 KFR_FUNCTION expression_make_function<fn::log, E1> log(E1&& x) 441 { 442 return { fn::log(), std::forward<E1>(x) }; 443 } 444 445 /// @brief Returns the binary (base-2) logarithm of the x. Accepts and returns expressions. 446 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 447 KFR_FUNCTION expression_make_function<fn::log2, E1> log2(E1&& x) 448 { 449 return { fn::log2(), std::forward<E1>(x) }; 450 } 451 452 /// @brief Returns the common (base-10) logarithm of the x. Accepts and returns expressions. 453 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 454 KFR_FUNCTION expression_make_function<fn::log10, E1> log10(E1&& x) 455 { 456 return { fn::log10(), std::forward<E1>(x) }; 457 } 458 459 /// @brief Returns the rounded binary (base-2) logarithm of the x. Version that accepts and returns 460 /// expressions. 461 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 462 KFR_FUNCTION expression_make_function<fn::logb, E1> logb(E1&& x) 463 { 464 return { fn::logb(), std::forward<E1>(x) }; 465 } 466 467 /// @brief Returns the logarithm of the x with base y. Accepts and returns expressions. 468 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 469 KFR_FUNCTION expression_make_function<fn::logn, E1, E2> logn(E1&& x, E2&& y) 470 { 471 return { fn::logn(), std::forward<E1>(x), std::forward<E2>(y) }; 472 } 473 474 /// @brief Returns log(x) * y. Accepts and returns expressions. 475 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 476 KFR_FUNCTION expression_make_function<fn::logm, E1, E2> logm(E1&& x, E2&& y) 477 { 478 return { fn::logm(), std::forward<E1>(x), std::forward<E2>(y) }; 479 } 480 481 /// @brief Returns exp(x * m + a). Accepts and returns expressions. 482 template <typename E1, typename E2, typename E3, KFR_ACCEPT_EXPRESSIONS(E1, E2, E3)> 483 KFR_FUNCTION expression_make_function<fn::exp_fmadd, E1, E2, E3> exp_fmadd(E1&& x, E2&& y, E3&& z) 484 { 485 return { fn::exp_fmadd(), std::forward<E1>(x), std::forward<E2>(y), std::forward<E3>(z) }; 486 } 487 488 /// @brief Returns log(x) * m + a. Accepts and returns expressions. 489 template <typename E1, typename E2, typename E3, KFR_ACCEPT_EXPRESSIONS(E1, E2, E3)> 490 KFR_FUNCTION expression_make_function<fn::log_fmadd, E1, E2, E3> log_fmadd(E1&& x, E2&& y, E3&& z) 491 { 492 return { fn::log_fmadd(), std::forward<E1>(x), std::forward<E2>(y), std::forward<E3>(z) }; 493 } 494 495 /// @brief Returns the x raised to the given power y. Accepts and returns expressions. 496 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 497 KFR_FUNCTION expression_make_function<fn::pow, E1, E2> pow(E1&& x, E2&& y) 498 { 499 return { fn::pow(), std::forward<E1>(x), std::forward<E2>(y) }; 500 } 501 502 /// @brief Returns the real nth root of the x. Accepts and returns expressions. 503 template <typename E1, typename E2, KFR_ACCEPT_EXPRESSIONS(E1, E2)> 504 KFR_FUNCTION expression_make_function<fn::root, E1, E2> root(E1&& x, E2&& y) 505 { 506 return { fn::root(), std::forward<E1>(x), std::forward<E2>(y) }; 507 } 508 509 /// @brief Returns the cube root of the x. Accepts and returns expressions. 510 template <typename E1, KFR_ACCEPT_EXPRESSIONS(E1)> 511 KFR_FUNCTION expression_make_function<fn::cbrt, E1> cbrt(E1&& x) 512 { 513 return { fn::cbrt(), std::forward<E1>(x) }; 514 } 515 516 } // namespace kfr