mono-complex.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * \file
  3. * C99 Complex math cross-platform support code
  4. *
  5. * Author:
  6. * Joao Matos (joao.matos@xamarin.com)
  7. *
  8. * Copyright 2015 Xamarin, Inc (http://www.xamarin.com)
  9. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  10. */
  11. #include <config.h>
  12. #include <glib.h>
  13. #define _USE_MATH_DEFINES // needed by MSVC to define math constants
  14. #include <math.h>
  15. #ifndef ENABLE_NETCORE
  16. typedef struct double_complex {
  17. double real;
  18. double imag;
  19. } double_complex;
  20. #define mono_creal(c) ((c).real)
  21. #define mono_cimag(c) ((c).imag)
  22. static inline
  23. double_complex mono_double_complex_make(gdouble re, gdouble im)
  24. {
  25. double_complex const a = { re, im };
  26. return a;
  27. }
  28. static inline
  29. double_complex mono_double_complex_scalar_div(double_complex c, gdouble s)
  30. {
  31. return mono_double_complex_make (mono_creal (c) / s, mono_cimag (c) / s);
  32. }
  33. static inline
  34. double_complex mono_double_complex_scalar_mul(double_complex c, gdouble s)
  35. {
  36. return mono_double_complex_make (mono_creal (c) * s, mono_cimag (c) * s);
  37. }
  38. static inline
  39. double_complex mono_double_complex_div(double_complex left, double_complex right)
  40. {
  41. double denom = mono_creal (right) * mono_creal (right) + mono_cimag (right) * mono_cimag (right);
  42. return mono_double_complex_make(
  43. (mono_creal (left) * mono_creal (right) + mono_cimag (left) * mono_cimag (right)) / denom,
  44. (-mono_creal (left) * mono_cimag (right) + mono_cimag (left) * mono_creal (right)) / denom);
  45. }
  46. static inline
  47. double_complex mono_double_complex_sub(double_complex left, double_complex right)
  48. {
  49. return mono_double_complex_make (mono_creal (left) - mono_creal (right), mono_cimag (left)
  50. - mono_cimag (right));
  51. }
  52. #include "../../support/libm/complex.c"
  53. #endif