summaryrefslogtreecommitdiffstats
path: root/WDL/cmath/test_eval.c
blob: fabf9ff1f39baf28a66ea29c238981925b00d1da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
  test_eval.h
  Copyright (C) 2011 and later Lubomir I. Ivanov (neolit123 [at] gmail)

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

/*
  test horner.h for complex numbers and other related headers

  gcc -W -Wall -Wextra -ansi pedantic
  cl /W4 /Za

  reduced precisions for ansi c
*/

#include "stdio.h"
#include "complex_number.h"
#include "horner.h"
#include "durand_kerner.h"

int main(void)
{
  cmath_uint16_t i = 0;

  cnum_t y[] = {2, -6, 2, -1};
  cnum_s cy[] = {{2, 0}, {-6, 0}, {2, 0}, {-1, 0}};

  cnum_t fx = horner_eval(y, 5, poly_order(y));
  cnum_s fcx = horner_eval_c(cy, _CNUM(5, 0), poly_order(y));

  cnum_t dk_coeff[] = {12, -7, 0.001, 0, 3, -5};
  cnum_s dk_roots[5];

  cnum_s dk_coeff_c[] = {{12, 0}, {-7, 0}, {0.001, 0}, {0, 0}, {3, 0}, {-5, 0}};
  cnum_s dk_roots_c[5];

  durand_kerner(dk_coeff, dk_roots, poly_order(dk_coeff));
  durand_kerner_c(dk_coeff_c, dk_roots_c, poly_order(dk_coeff_c));

  /* */
  #ifdef _CMATH_ANSI
    puts("\n\nansi c is: on");
  #else
    puts("\n\nansi c is: off");
  #endif

  /* */
  puts("\n\nevaluate polynomials:\n");  
  printf("* y[]: %.15f\n", (double)fx);  
  printf("* cy[]: %.15f \t %.15f*i\n", (double)fcx.r, (double)fcx.i);

  /* */
  puts("\nfind roots:");
  puts("\n* dk_coeff[]:");  
  i = 0;
  while (i < poly_order(dk_coeff))
  {
    printf("root[%2d]: %.15f \t % .15f*i\n",
      i+1, (double)dk_roots[i].r, (double)dk_roots[i].i);
    i++;
  }  
  i = 0;
  puts("\n* dk_coeff_c[]:");
  while (i < poly_order(dk_coeff_c))
  {
    printf("root[%2d]: %.15f \t % .15f*i\n",
      i+1, (double)dk_roots_c[i].r, (double)dk_roots_c[i].i);
    i++;
  }

  return 0;
}