Friday, 15 December 2017
arrow JDiceChecker 5.0.0.1 arrow RandomTest arrow MathematicalFunctions.java (2)
MathematicalFunctions.java (2nd part) (mathematical functions class) Print E-mail
Get C++ DiceLock cipher architecture source code packages of DiceLock for Microsoft Visual Studio 2013 and DiceLock-x for Linux with Test Driver Programs and Response Test Vector files to verify that both them work as expected.
DiceLock for Windows and Linux
DiceLock Security governing software licenses are Free/Libre Source Code License and Educational and Research License
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
 
  /**
   * Evaluate polynomial of degree N
   * 
   * @param     x         x value to evaluate polynomial
   * @param     coef      coef array of double values to evaluate polynomial
   * @param     N         N degree to evaluate polynomial
   * @return    double:   polynomial of degree N of values "x" and "coef" array
   */ 
    public double PolEvl(double x, double coef[], int N) {
      double ans;
      double p[];
      int i;
      int index;
 
      index = 0;
      p = coef;
      ans = p[index++];
      i = N;
      do
        ans = ans * x + p[index++];
      while ( (--i) != 0);
      return (ans);
    }
 
  /**
   * Evaluate polynomial when coefficient of x  is 1.0.
   * 
   * @param     x         x value to evaluate polynomial when it is 1.0
   * @param     coef      coef array of double values to evaluate polynomial when x value is 1.0
   * @param     N         N degree to evaluate polynomial when x value is 1.0
   * @return    double:   polynomial of degree N of values "x" and "coef" array when x value is 1.0
   */ 
    public double P1Evl(double x, double coef[], int N) {
      double ans;
      double p[];
      int i;
      int index;
 
      index = 0;
      p = coef;
      ans = x + p[index++];
      i = N-1;
      do
        ans = ans * x + p[index++];
      while ( (--i) != 0);
      return (ans);
    }
    
  /**
   * Error function in double precision 
   * 
   * @param     x         x value to evaluate error function in double precision 
   * @return    double:   error function in double precision of value "x"
   */ 
    public double ErF(double x) {
      final double TWO_SQRTPI = 1.128379167095512574;
      final double REL_ERROR = 1E-12;
      double  sum = x, term = x, xsqr = x * x;
      int   j = 1;
 
      if ( Math.abs(x) > 2.2 )
        return 1.0 - this.ErFc(x);
      do {
        term *= xsqr/j;
        sum -= term/(2*j+1);
        j++;
        term *= xsqr/j;
        sum += term/(2*j+1);
        j++;
      } while ( Math.abs(term)/sum > REL_ERROR );
      return TWO_SQRTPI * sum;
    }
     
  /**
   * Error function in double precision 
   * 
   * @param     x         x value to evaluate error function in double precision 
   * @return    double:   error function in double precision of value "x"
   */ 
    public double ErFc(double x) {
      final double ONE_SQRTPI = 0.564189583547756287;
      final double REL_ERROR = 1E-12;
      double  a = 1, b = x, c = x, d = x*x + 0.5;
      double  q1, q2 = b/d, n = 1.0, t;
 
      if ( Math.abs(x) < 2.2 )
        return 1.0 - this.ErF(x);
      if ( x < 0 )
        return 2.0 - this.ErFc(-x);
      do {
        t = a*n + b*x;
        a = b;
        b = t;
        t = c*n + d*x;
        c = d;
        d = t;
        n += 0.5;
        q1 = q2;
        q2 = b/d;
      } while ( Math.abs(q1-q2)/q2 > REL_ERROR );
      return ONE_SQRTPI * Math.exp(-x*x) * q2;
    }
 
  /**
   * Statistical Normal function
   * 
   * @param     x         x value to evaluate statistical normal function
   * @return    double:   statistical normal function of value "x"
   */ 
    public double Normal(double x) {
      final double SQRT2 = 1.414213562373095048801688724209698078569672;
      double arg, result;
 
      if (x > 0) {
        arg = x/SQRT2;
        result = 0.5 * ( 1 + this.ErF(arg) );
      } 
      else {
        arg = -x/SQRT2;
        result = 0.5 * ( 1 - this.ErF(arg) );
      }
      return(result);
    }
 
  /**
   * Get mathematical error boolean if last executed function failed
   * 
   * @return    boolean:   boolean indicating if last executed function produced an error
   */ 
    public boolean GetError() {
 
      return this.error;
    }
 
  /**
   * Get mathematical error boolean if last executed function failed
   * 
   * @return    MathematicalErrors:   MathematicalErrors enumeration indicating error of last executed function
   */ 
    public MathematicalErrors GetMathError() {
 
      return this.mathError;
    }
    
    // DEFINES AS METHODS
  /**
   * Returns the greater of two double values
   * 
   * @param     x         double x value to evaluate greater value
   * @param     y         double y value to evaluate greater value
   * @return    double:   returns greater value of "x" and "y"
   */ 
    public double max(double x, double y) {
        
      return ((x) <  (y)  ? (y)  : (x));
    }
 
  /**
   * Returns the lesser of two double values
   * 
   * @param     x         double x value to evaluate greater value
   * @param     y         double y value to evaluate greater value
   * @return    double:   returns greater value of "x" and "y"
   */ 
    public double min(double x,double y) {
 
      return ((x) >  (y)  ? (y)  : (x));
    }
 
  /**
   * Returns if a double is non positive
   * 
   * @param     x   double x value to evaluate if it is non positive
   * @return    boolean:   true:   if it is non positive 
   *                       false:  if it is positive 
   */ 
    public double isNonPositive(double x) {
 
      return ((x) <= 0.e0 ?   1  : 0);
    }
 
  /**
   * Returns if a double is positive
   * 
   * @param     x   double x value to evaluate if it is positive
   * @return    boolean:    true:   if it is positive 
   *                        false:  if it is non positive 
   */ 
    public boolean isPositive(double x) {
 
      return ((x) >  0.e0 ?   true : false);
    }
 
  /**
   * Returns if a double is negative
   * 
   * @param     x   double x value to evaluate if it is negative
   * @return    boolean:    true:   if it is negative 
   *                        false:  if it is non negative
   */ 
    public boolean isNegative(double x) {
 
      return ((x) <  0.e0 ?   true : false);
    }
 
  /**
   * Returns if a double is greater than 1
   * 
   * @param     x   double x value to evaluate if it is greater than 1
   * @return    boolean:    true:   if it is greater than 1
   *                        false:  if it is non greater than 1
   */ 
    public boolean isGreaterThanOne(double x) {
 
      return ((x) >  1.e0 ?   true : false);
    }
 
  /**
   * Returns if a double equals 0
   * 
   * @param     x   double x value to evaluate if it equals 0
   * @return    boolean:    true:   if it equals 0
   *                        false:  if it no equals 0
   */ 
    public boolean isZero(double x) {
 
      return ((x) == 0.e0 ?   true : false);
    }
 
  /**
   * Returns if a double equals 1
   * 
   * @param     x   double x value to evaluate if it equals 1
   * @return    boolean:    true:   if it equals 1
   *                        false:  if it no equals 1
   */ 
    public boolean isOne(double x) {
 
      return ((x) == 1.e0 ?   true : false);
    }
 
}