source: src/main/java/agents/anac/y2015/agentBuyogV2/flanagan/analysis/ErrorProp.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 39.3 KB
Line 
1/*
2* Class ErrorProp
3*
4* Defines an object consisting of a variable and its associated standard
5* deviation and the class includes the methods for propagating the error
6* in standard arithmetic operations for both correlated and uncorrelated
7* errors.
8*
9* WRITTEN BY: Dr Michael Thomas Flanagan
10*
11* DATE: October 2002
12* UPDATE: 26 April 2004, 19 January 2005
13*
14* See ComplexErrorProp for the propogation of errors in complex arithmetic
15*
16* DOCUMENTATION:
17* See Michael Thomas Flanagan's Java library on-line web page:
18* http://www.ee.ucl.ac.uk/~mflanaga/java/ErrorProp.html
19* http://www.ee.ucl.ac.uk/~mflanaga/java/
20*
21* Copyright (c) 2002 - 2008 Michael Thomas Flanagan
22*
23* PERMISSION TO COPY:
24*
25* Redistributions of this source code, or parts of, must retain the above
26* copyright notice, this list of conditions and the following disclaimer.
27*
28* Redistribution in binary form of all or parts of this class, must reproduce
29* the above copyright, this list of conditions and the following disclaimer in
30* the documentation and/or other materials provided with the distribution.
31*
32* Permission to use, copy and modify this software and its documentation for
33* NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
34* to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all
35* copies and associated documentation or publications.
36*
37* Dr Michael Thomas Flanagan makes no representations about the suitability
38* or fitness of the software for any or for a particular purpose.
39* Michael Thomas Flanagan shall not be liable for any damages suffered
40* as a result of using, modifying or distributing this software or its derivatives.
41*
42***************************************************************************************/
43
44package agents.anac.y2015.agentBuyogV2.flanagan.analysis;
45
46import agents.anac.y2015.agentBuyogV2.flanagan.math.Fmath;
47
48public class ErrorProp{
49
50 // DATA VARIABLES
51 private double value = 0.0D; // number value
52 private double error = 0.0D; // its standard deviation or an estimate of its standard deviation
53
54
55 // CONSTRUCTORS
56 // default constructor
57 public ErrorProp(){
58 value = 0.0;
59 error = 0.0;
60 }
61
62 // constructor with value and error initialised
63 public ErrorProp(double value, double error){
64 this.value = value;
65 this.error = error;
66 }
67
68 // constructor with value initialised
69 public ErrorProp(double value){
70 this.value = value;
71 this.error = 0.0;
72 }
73
74 // PUBLIC METHODS
75
76 // SET VALUES
77 // Set the value of value
78 public void setValue(double value){
79 this.value = value;
80 }
81
82 // Set the value of error
83 public void setError(double error){
84 this.error = error;
85 }
86
87 // Set the values of value and error
88 public void reset(double value, double error){
89 this.value = value;
90 this.error = error;
91 }
92
93 // GET VALUES
94 // Get the value of value
95 public double getValue(){
96 return value;
97 }
98
99 // Get the value of error
100 public double getError(){
101 return error;
102 }
103
104 //PRINT AN ERROR NUMBER
105 // Print to terminal window with text (message) and a line return
106 public void println(String message){
107 System.out.println(message + " " + this.toString());
108 }
109
110 // Print to terminal window without text (message) but with a line return
111 public void println(){
112 System.out.println(" " + this.toString());
113 }
114
115 // Print to terminal window with text (message) but without line return
116 public void print(String message){
117 System.out.print(message + " " + this.toString());
118 }
119
120 // Print to terminal window without text (message) and without line return
121 public void print(){
122 System.out.print(" " + this.toString());
123 }
124
125 // TRUNCATION
126 // Rounds the mantissae of both the value and error parts of Errorprop to prec places
127 public static ErrorProp truncate(ErrorProp x, int prec){
128 if(prec<0)return x;
129
130 double xV = x.getValue();
131 double xS = x.getError();
132 ErrorProp y = new ErrorProp();
133
134 xV = Fmath.truncate(xV, prec);
135 xS = Fmath.truncate(xS, prec);
136
137 y.reset(xV, xS);
138
139 return y;
140 }
141
142 // instance method
143 public ErrorProp truncate(int prec){
144 if(prec<0)return this;
145
146 double xV = this.getValue();
147 double xS = this.getError();
148 ErrorProp y = new ErrorProp();
149
150 xV = Fmath.truncate(xV, prec);
151 xS = Fmath.truncate(xS, prec);
152
153 y.reset(xV, xS);
154
155 return y;
156 }
157
158 // CONVERSIONS
159 // Format an ErrorProp number as a string
160 // Overides java.lang.String.toString()
161 public String toString(){
162 return this.value+", error = "+this.error;
163 }
164
165 // Format an ErrorProp number as a string
166 // See static method above for comments
167 public static String toString(ErrorProp aa){
168 return aa.value+", error = "+aa.error;
169 }
170
171 // Return a HASH CODE for the ErrorProp number
172 // Overides java.lang.Object.hashCode()
173 public int hashCode(){
174 long lvalue = Double.doubleToLongBits(this.value);
175 long lerror = Double.doubleToLongBits(this.error);
176 int hvalue = (int)(lvalue^(lvalue>>>32));
177 int herror = (int)(lerror^(lerror>>>32));
178 return 7*(hvalue/10)+3*(herror/10);
179 }
180
181
182 // ARRAYS
183
184 // Create a one dimensional array of ErrorProp objects of length n
185 // all values = 0 and all error's = 0
186 public static ErrorProp[] oneDarray(int n){
187 ErrorProp[] a =new ErrorProp[n];
188 for(int i=0; i<n; i++){
189 a[i]=ErrorProp.zero();
190 }
191 return a;
192 }
193
194 // Create a one dimensional array of ErrorProp objects of length n
195 // all values = a and all error's = b
196 public static ErrorProp[] oneDarray(int n, double a, double b){
197 ErrorProp[] c =new ErrorProp[n];
198 for(int i=0; i<n; i++){
199 c[i]=ErrorProp.zero();
200 c[i].reset(a, b);
201 }
202 return c;
203 }
204
205 // Create a one dimensional array of ErrorProp objects of length n
206 // all = the ErrorProp number named constant
207 public static ErrorProp[] oneDarray(int n, ErrorProp constant){
208 ErrorProp[] c =new ErrorProp[n];
209 for(int i=0; i<n; i++){
210 c[i]=ErrorProp.copy(constant);
211 }
212 return c;
213 }
214
215 // Create a two dimensional array of ErrorProp objects of dimensions n and m
216 // all values = zero and all error's = zero
217 public static ErrorProp[][] twoDarray(int n, int m){
218 ErrorProp[][] a =new ErrorProp[n][m];
219 for(int i=0; i<n; i++){
220 for(int j=0; j<m; j++){
221 a[i][j]=ErrorProp.zero();
222 }
223 }
224 return a;
225 }
226
227 // Create a two dimensional array of ErrorProp objects of dimensions n and m
228 // all values = a and all error's = b
229 public static ErrorProp[][] twoDarray(int n, int m, double a, double b){
230 ErrorProp[][] c =new ErrorProp[n][m];
231 for(int i=0; i<n; i++){
232 for(int j=0; j<m; j++){
233 c[i][j]=ErrorProp.zero();
234 c[i][j].reset(a, b);
235 }
236 }
237 return c;
238 }
239
240 // Create a two dimensional array of ErrorProp objects of dimensions n and m
241 // all = the ErrorProp number named constant
242 public static ErrorProp[][] twoDarray(int n, int m, ErrorProp constant){
243 ErrorProp[][] c =new ErrorProp[n][m];
244 for(int i=0; i<n; i++){
245 for(int j=0; j<m; j++){
246 c[i][j]=ErrorProp.copy(constant);
247 }
248 }
249 return c;
250 }
251
252 // COPY
253 // Copy a single ErrorProp number [static method]
254 public static ErrorProp copy(ErrorProp a){
255 ErrorProp b = new ErrorProp();
256 b.value = a.value;
257 b.error = a.error;
258 return b;
259 }
260
261 // Copy a single ErrorProp number [instance method]
262 public ErrorProp copy(){
263 ErrorProp b = new ErrorProp();
264 b.value = this.value;
265 b.error = this.error;
266 return b;
267 }
268
269 // Clone a single ErrorProp number
270 public Object clone(){
271 ErrorProp b = new ErrorProp();
272 b.value = this.value;
273 b.error = this.error;
274 return (Object) b;
275 }
276
277
278 // Copy a 1D array of ErrorProp numbers (deep copy)
279 public static ErrorProp[] copy(ErrorProp[] a){
280 int n =a.length;
281 ErrorProp[] b = ErrorProp.oneDarray(n);
282 for(int i=0; i<n; i++){
283 b[i]=ErrorProp.copy(a[i]);
284 }
285 return b;
286 }
287
288 // Copy a 2D array of ErrorProp numbers (deep copy)
289 public static ErrorProp[][] copy(ErrorProp[][] a){
290 int n =a.length;
291 int m =a[0].length;
292 ErrorProp[][] b = ErrorProp.twoDarray(n, m);
293 for(int i=0; i<n; i++){
294 for(int j=0; j<m; j++){
295 b[i][j]=ErrorProp.copy(a[i][j]);
296 }
297 }
298 return b;
299 }
300
301 // ADDITION
302 // Add two ErrorProp numbers with correlation [instance method]
303 public ErrorProp plus(ErrorProp a, double corrCoeff){
304 ErrorProp c = new ErrorProp();
305 c.value=a.value+this.value;
306 c.error = hypotWithCov(a.error, this.error, corrCoeff);
307 return c;
308 }
309
310 // Add two ErrorProp numbers with correlation [static method]
311 public static ErrorProp plus(ErrorProp a, ErrorProp b, double corrCoeff){
312 ErrorProp c = new ErrorProp();
313 c.value=a.value+b.value;
314 c.error = hypotWithCov(a.error, b.error, corrCoeff);
315 return c;
316 }
317
318 //Add a ErrorProp number to this ErrorProp number with no, i.e. zero, correlation [instance method]
319 // this ErrorProp number remains unaltered
320 public ErrorProp plus(ErrorProp a ){
321 ErrorProp b = new ErrorProp();
322 b.value = this.value + a.value;
323 b.error = hypotWithCov(a.error, this.error, 0.0D);
324 return b;
325 }
326
327 // Add two ErrorProp numbers with no, i.e. zero, correlation term [static method]
328 public static ErrorProp plus(ErrorProp a, ErrorProp b){
329 ErrorProp c = new ErrorProp();
330 c.value=a.value+b.value;
331 c.error = hypotWithCov(a.error, b.error, 0.0D);
332 return c;
333 }
334
335 // Add an error free double number to this ErrorProp number [instance method]
336 // this ErrorProp number remains unaltered
337 public ErrorProp plus(double a ){
338 ErrorProp b = new ErrorProp();
339 b.value = this.value + a;
340 b.error = Math.abs(this.error);
341 return b;
342 }
343
344 //Add a ErrorProp number to an error free double [static method]
345 public static ErrorProp plus(double a, ErrorProp b){
346 ErrorProp c = new ErrorProp();
347 c.value=a+b.value;
348 c.error=Math.abs(b.error);
349 return c;
350 }
351
352 //Add an error free double number to an error free double and return sum as ErrorProp [static method]
353 public static ErrorProp plus(double a, double b){
354 ErrorProp c = new ErrorProp();
355 c.value=a+b;
356 c.error=0.0D;
357 return c;
358 }
359
360
361 // Add a ErrorProp number to this ErrorProp number and replace this with the sum
362 // with correlation term
363 public void plusEquals(ErrorProp a, double corrCoeff){
364 this.value+=a.value;
365 this.error = hypotWithCov(a.error, this.error, corrCoeff);
366 }
367
368 // Add a ErrorProp number to this ErrorProp number and replace this with the sum
369 // with no, i.e. zero, correlation term
370 public void plusEquals(ErrorProp a){
371 this.value+=a.value;
372 this.error = Math.sqrt(a.error*a.error + this.error*this.error);
373 this.error = hypotWithCov(a.error, this.error, 0.0D);
374 }
375
376 //Add double number to this ErrorProp number and replace this with the sum
377 public void plusEquals(double a ){
378 this.value+=a;
379 this.error=Math.abs(this.error);
380 }
381
382 // SUBTRACTION
383 // Subtract an ErrorProp number from this ErrorProp number with correlation [instance method]
384 // this ErrorProp number remains unaltered
385 public ErrorProp minus(ErrorProp a, double corrCoeff){
386 ErrorProp c = new ErrorProp();
387 c.value=this.value-a.value;
388 c.error = hypotWithCov(this.error, a.error, -corrCoeff);
389 return c;
390 }
391
392 // Subtract ErrorProp number b from ErrorProp number a with correlation [static method]
393 public static ErrorProp minus(ErrorProp a, ErrorProp b, double corrCoeff){
394 ErrorProp c = new ErrorProp();
395 c.value=a.value-b.value;
396 c.error = hypotWithCov(a.error, b.error, -corrCoeff);
397 return c;
398 }
399
400 // Subtract a ErrorProp number from this ErrorProp number with no, i.e. zero, correlation [instance method]
401 // this ErrorProp number remains unaltered
402 public ErrorProp minus(ErrorProp a ){
403 ErrorProp b = new ErrorProp();
404 b.value = this.value - a.value;
405 b.error = hypotWithCov(a.error, this.error, 0.0D);
406 return b;
407 }
408
409 // Subtract ErrorProp number b from ErrorProp number a with no, i.e. zero, correlation term [static method]
410 public static ErrorProp minus(ErrorProp a, ErrorProp b){
411 ErrorProp c = new ErrorProp();
412 c.value=a.value-b.value;
413 c.error = hypotWithCov(a.error, b.error, 0.0D);
414 return c;
415 }
416
417 // Subtract an error free double number from this ErrorProp number [instance method]
418 // this ErrorProp number remains unaltered
419 public ErrorProp minus(double a ){
420 ErrorProp b = new ErrorProp();
421 b.value = this.value - a;
422 b.error = Math.abs(this.error);
423 return b;
424 }
425
426 // Subtract a ErrorProp number b from an error free double a [static method]
427 public static ErrorProp minus(double a, ErrorProp b){
428 ErrorProp c = new ErrorProp();
429 c.value=a-b.value;
430 c.error=Math.abs(b.error);
431 return c;
432 }
433
434 //Subtract an error free double number b from an error free double a and return sum as ErrorProp [static method]
435 public static ErrorProp minus(double a, double b){
436 ErrorProp c = new ErrorProp();
437 c.value=a-b;
438 c.error=0.0D;
439 return c;
440 }
441
442 // Subtract a ErrorProp number to this ErrorProp number and replace this with the sum
443 // with correlation term
444 public void minusEquals(ErrorProp a, double corrCoeff){
445 this.value-=a.value;
446 this.error = hypotWithCov(a.error, this.error, -corrCoeff);
447 }
448
449 // Subtract a ErrorProp number from this ErrorProp number and replace this with the sum
450 // with no, i.e. zero, correlation term
451 public void minusEquals(ErrorProp a){
452 this.value-=a.value;
453 this.error = hypotWithCov(a.error, this.error, 0.0D);
454 }
455
456 // Subtract a double number from this ErrorProp number and replace this with the sum
457 public void minusEquals(double a){
458 this.value-=a;
459 this.error=Math.abs(this.error);
460 }
461
462 // MULTIPLICATION
463 //Multiply two ErrorProp numbers with correlation [instance method]
464 public ErrorProp times(ErrorProp a, double corrCoeff){
465 ErrorProp c = new ErrorProp();
466 double cov = corrCoeff*a.error*this.error;
467 c.value=a.value*this.value;
468 if(a.value==0.0D){
469 c.error=a.error*this.value;
470 }
471 else{
472 if(this.value==0.0D){
473 c.error=this.error*a.value;
474 }
475 else{
476 c.error = Math.abs(c.value)*hypotWithCov(a.error/a.value, this.error/this.value, corrCoeff);
477 }
478 }
479 return c;
480 }
481
482 //Multiply this ErrorProp number by a ErrorProp number [instance method]
483 // with no, i.e. zero, correlation
484 // this ErrorProp number remains unaltered
485 public ErrorProp times(ErrorProp a){
486 ErrorProp b = new ErrorProp();
487 b.value=this.value*a.value;
488 if(a.value==0.0D){
489 b.error=a.error*this.value;
490 }
491 else{
492 if(this.value==0.0D){
493 b.error=this.error*a.value;
494 }
495 else{
496 b.error = Math.abs(b.value)*hypotWithCov(a.error/a.value, this.error/this.value, 0.0D);
497 }
498 }
499 return b;
500 }
501
502 //Multiply this ErrorProp number by a double [instance method]
503 // this ErrorProp number remains unaltered
504 public ErrorProp times(double a){
505 ErrorProp b = new ErrorProp();
506 b.value=this.value*a;
507 b.error=Math.abs(this.error*a);
508 return b;
509 }
510
511
512 //Multiply two ErrorProp numbers with correlation [static method]
513 public static ErrorProp times(ErrorProp a, ErrorProp b, double corrCoeff){
514 ErrorProp c = new ErrorProp();
515 double cov = corrCoeff*a.error*b.error;
516 c.value=a.value*b.value;
517 if(a.value==0.0D){
518 c.error=a.error*b.value;
519 }
520 else{
521 if(b.value==0.0D){
522 c.error=b.error*a.value;
523 }
524 else{
525 c.error = Math.abs(c.value)*hypotWithCov(a.error/a.value, b.error/b.value, corrCoeff);
526 }
527 }
528 return c;
529 }
530
531 //Multiply two ErrorProp numbers with no, i.e. zero, correlation [static method]
532 public static ErrorProp times(ErrorProp a, ErrorProp b){
533 ErrorProp c = new ErrorProp();
534 c.value=a.value*b.value;
535 if(a.value==0.0D){
536 c.error=a.error*b.value;
537 }
538 else{
539 if(b.value==0.0D){
540 c.error=b.error*a.value;
541 }
542 else{
543 c.error = Math.abs(c.value)*hypotWithCov(a.error/a.value, b.error/b.value, 0.0D);
544 }
545 }
546 return c;
547 }
548
549 //Multiply a double by a ErrorProp number [static method]
550 public static ErrorProp times(double a, ErrorProp b){
551 ErrorProp c = new ErrorProp();
552 c.value=a*b.value;
553 c.error=Math.abs(a*b.error);
554 return c;
555 }
556
557 //Multiply a double number by a double and return product as ErrorProp [static method]
558 public static ErrorProp times(double a, double b){
559 ErrorProp c = new ErrorProp();
560 c.value=a*b;
561 c.error=0.0;
562 return c;
563 }
564
565 //Multiply this ErrorProp number by an ErrorProp number and replace this by the product
566 // with correlation
567 public void timesEquals(ErrorProp a, double corrCoeff){
568 ErrorProp b = new ErrorProp();
569 double cov = corrCoeff*this.error*a.error;
570 b.value = this.value*a.value;
571 if(a.value==0.0D){
572 b.error=a.error*this.value;
573 }
574 else{
575 if(this.value==0.0D){
576 b.error=this.error*a.value;
577 }
578 else{
579 b.error = Math.abs(b.value)*hypotWithCov(a.error/a.value, this.error/this.value, corrCoeff);
580 }
581 }
582
583 this.value = b.value;
584 this.error = b.error;
585 }
586
587 //Multiply this ErrorProp number by an ErrorProp number and replace this by the product
588 // with no, i.e. zero, correlation
589 public void timesEquals(ErrorProp a){
590 ErrorProp b = new ErrorProp();
591 b.value = this.value*a.value;
592 if(a.value==0.0D){
593 b.error=a.error*this.value;
594 }
595 else{
596 if(this.value==0.0D){
597 b.error=this.error*a.value;
598 }
599 else{
600 b.error = Math.abs(b.value)*hypotWithCov(a.error/a.value, this.error/this.value, 0.0D);
601 }
602 }
603
604 this.value = b.value;
605 this.error = b.error;
606 }
607
608 //Multiply this ErrorProp number by a double and replace this by the product
609 public void timesEquals(double a){
610 this.value=this.value*a;
611 this.error=Math.abs(this.error*a);
612 }
613
614 // DIVISION
615 // Division of this ErrorProp number by a ErrorProp number [instance method]
616 // this ErrorProp number remains unaltered
617 // with correlation
618 public ErrorProp over(ErrorProp a, double corrCoeff){
619 ErrorProp c = new ErrorProp();
620 c.value = this.value/a.value;
621 if(this.value==0.0D){
622 c.error=this.error*a.value;
623 }
624 else{
625 c.error = Math.abs(c.value)*hypotWithCov(this.error/this.value, a.error/a.value, -corrCoeff);
626 }
627 return c;
628 }
629
630 // Division of two ErrorProp numbers a/b [static method]
631 // with correlation
632 public static ErrorProp over(ErrorProp a, ErrorProp b, double corrCoeff){
633 ErrorProp c = new ErrorProp();
634 c.value = a.value/b.value;
635 if(a.value==0.0D){
636 c.error=a.error*b.value;
637 }
638 else{
639 c.error = Math.abs(c.value)*hypotWithCov(a.error/a.value, b.error/b.value, -corrCoeff);
640 }
641 return c;
642 }
643
644 // Division of this ErrorProp number by a ErrorProp number [instance method]
645 // this ErrorProp number remains unaltered
646 // with no, i.e. zero, correlation
647 public ErrorProp over(ErrorProp a){
648 ErrorProp b = new ErrorProp();
649 b.value = this.value/a.value;
650 b.error = Math.abs(b.value)*hypotWithCov(a.error/a.value, this.error/this.value, 0.0);
651 if(this.value==0.0D){
652 b.error=this.error*b.value;
653 }
654 else{
655 b.error = Math.abs(b.value)*hypotWithCov(a.error/a.value, this.error/this.value, 0.0);
656 }
657 return b;
658 }
659
660 // Division of two ErrorProp numbers a/b [static method]
661 // with no, i.e. zero, correlation
662 public static ErrorProp over(ErrorProp a, ErrorProp b){
663 ErrorProp c = new ErrorProp();
664 c.value = a.value/b.value;
665 if(a.value==0.0D){
666 c.error=a.error*b.value;
667 }
668 else{
669 c.error = Math.abs(c.value)*hypotWithCov(a.error/a.value, b.error/b.value, 0.0D);
670 }
671
672 return c;
673 }
674
675 //Division of this ErrorProp number by a double [instance method]
676 // this ErrorProp number remains unaltered
677 public ErrorProp over(double a){
678 ErrorProp b = new ErrorProp();
679 b.value=this.value/a;
680 b.error=Math.abs(this.error/a);
681 return b;
682 }
683
684
685 // Division of a double, a, by a ErrorProp number, b [static method]
686 public static ErrorProp over(double a, ErrorProp b){
687 ErrorProp c = new ErrorProp();
688 c.value = a/b.value;
689 c.error = Math.abs(a*b.error/(b.value*b.value));
690 return c;
691 }
692
693 // Divide a double number by a double and return quotient as ErrorProp [static method]
694 public static ErrorProp over(double a, double b){
695 ErrorProp c = new ErrorProp();
696 c.value=a/b;
697 c.error=0.0;
698 return c;
699 }
700
701 // Division of this ErrorProp number by a ErrorProp number and replace this by the quotient
702 // with no, i.r. zero, correlation
703 public void overEquals(ErrorProp b){
704 ErrorProp c = new ErrorProp();
705 c.value = this.value/b.value;
706 if(this.value==0.0D){
707 c.error=this.error*b.value;
708 }
709 else{
710 c.error = Math.abs(c.value)*hypotWithCov(this.error/this.value, b.error/b.value, 0.0D);
711 }
712 this.value = c.value;
713 this.error = c.error;
714 }
715
716 // Division of this ErrorProp number by a ErrorProp number and replace this by the quotient
717 // with correlation
718 public void overEquals(ErrorProp b, double corrCoeff){
719 ErrorProp c = new ErrorProp();
720 c.value = this.value/b.value;
721 if(this.value==0.0D){
722 c.error=this.error*b.value;
723 }
724 else{
725 c.error = Math.abs(c.value)*hypotWithCov(this.error/this.value, b.error/b.value, -corrCoeff);
726 }
727 this.value = c.value;
728 this.error = c.error;
729 }
730
731 //Division of this ErrorProp number by a double and replace this by the quotient
732 public void overEquals(double a){
733 this.value=this.value/a;
734 this.error=Math.abs(this.error/a);
735 }
736
737 // RECIPROCAL
738 // Returns the reciprocal (1/a) of a ErrorProp number (a) [instance method]
739 public ErrorProp inverse(){
740 ErrorProp b = ErrorProp.over(1.0D, this);
741 return b;
742 }
743
744 // Returns the reciprocal (1/a) of a ErrorProp number (a) [static method]
745 public static ErrorProp inverse(ErrorProp a){
746 ErrorProp b = ErrorProp.over(1.0, a);
747 return b;
748 }
749
750 //FURTHER MATHEMATICAL FUNCTIONS
751
752 // Returns the length of the hypotenuse of a and b i.e. sqrt(a*a + b*b)
753 // where a and b are ErrorProp [without unecessary overflow or underflow]
754 // with correlation
755 public static ErrorProp hypot(ErrorProp a, ErrorProp b, double corrCoeff){
756 ErrorProp c = new ErrorProp();
757 c.value = Fmath.hypot(a.value, b.value);
758 c.error = Math.abs(hypotWithCov(a.error*a.value, b.error*b.value, corrCoeff)/c.value);
759 return c;
760 }
761
762 // Returns the length of the hypotenuse of a and b i.e. sqrt(a*a + b*b)
763 // where a and b are ErrorProp [without unecessary overflow or underflow]
764 // with no, i.e. zero, correlation
765 public static ErrorProp hypot(ErrorProp a, ErrorProp b){
766 ErrorProp c = new ErrorProp();
767 c.value = Fmath.hypot(a.value, b.value);
768 c.error = Math.abs(hypotWithCov(a.error*a.value, b.error*b.value, 0.0D)/c.value);
769 return c;
770 }
771
772 //Absolute value [static method]
773 public static ErrorProp abs(ErrorProp a){
774 ErrorProp b = new ErrorProp();
775 b.value = Math.abs(a.value);
776 b.error = Math.abs(a.error);
777 return b;
778 }
779
780 //Absolute value [instance method]
781 public ErrorProp abs(){
782 ErrorProp b = new ErrorProp();
783 b.value = Math.abs(this.value);
784 b.error = Math.abs(this.error);
785 return b;
786 }
787
788 // Exponential
789 public static ErrorProp exp(ErrorProp a){
790 ErrorProp b = new ErrorProp();
791 b.value = Math.exp(a.value);
792 b.error = Math.abs(b.value*a.error);
793 return b;
794 }
795
796 // Natural log
797 public static ErrorProp log(ErrorProp a){
798 ErrorProp b = new ErrorProp();
799 b.value = Math.log(a.value);
800 b.error = Math.abs(a.error/a.value);
801 return b;
802 }
803
804 // log to base 10
805 public static ErrorProp log10(ErrorProp a){
806 ErrorProp b = new ErrorProp();
807 b.value = Fmath.log10(a.value);
808 b.error = Math.abs(a.error/(a.value*Math.log(10.0D)));
809 return b;
810 }
811
812 //Roots
813 // Square root
814 public static ErrorProp sqrt(ErrorProp a){
815 ErrorProp b = new ErrorProp();
816 b.value = Math.sqrt(a.value);
817 b.error = Math.abs(a.error/(2.0D*a.value));
818 return b;
819 }
820
821 // The nth root (n = integer > 1)
822 public static ErrorProp nthRoot(ErrorProp a, int n){
823 if(n==0)throw new ArithmeticException("Division by zero (n = 0 - infinite root) attempted in ErrorProp.nthRoot");
824 ErrorProp b = new ErrorProp();
825 b.value = Math.pow(a.value, 1/n);
826 b.error = Math.abs(a.error*Math.pow(a.value, 1/n-1)/((double)n));
827 return b;
828 }
829
830 //Powers
831 //Square [instance method]
832 public ErrorProp square(){
833 ErrorProp a = new ErrorProp(this.value, this.error);
834 return a.times(a, 1.0D);
835 }
836
837 //Square [static method]
838 public static ErrorProp square(ErrorProp a){
839 return a.times(a,1.0D);
840 }
841
842 // returns an ErrorProp number raised to an error free power
843 public static ErrorProp pow(ErrorProp a, double b){
844 ErrorProp c = new ErrorProp();
845 c.value = Math.pow(a.value, b);
846 c.error = Math.abs(b*Math.pow(a.value, b-1.0));
847 return c;
848 }
849
850 // returns an error free number raised to an ErrorProp power
851 public static ErrorProp pow(double a, ErrorProp b){
852 ErrorProp c = new ErrorProp();
853 c.value = Math.pow(a, b.value);
854 c.error = Math.abs(c.value*Math.log(a)*b.error);
855 return c;
856 }
857
858 // returns a ErrorProp number raised to a ErrorProp power
859 // with correlation
860 public static ErrorProp pow(ErrorProp a, ErrorProp b, double corrCoeff){
861 ErrorProp c = new ErrorProp();
862 c.value = Math.pow(a.value, b.value);
863 c.error = hypotWithCov(a.error*b.value*Math.pow(a.value, b.value-1.0), b.error*Math.log(a.value)*Math.pow(a.value, b.value), corrCoeff);
864 return c;
865 }
866
867 // returns a ErrorProp number raised to a ErrorProp power
868 // with zero correlation
869 public static ErrorProp pow(ErrorProp a, ErrorProp b){
870 ErrorProp c = new ErrorProp();
871 c.value = Math.pow(a.value, b.value);
872 c.error = hypotWithCov(a.error*b.value*Math.pow(a.value, b.value-1.0), b.error*Math.log(a.value)*Math.pow(a.value, b.value), 0.0D);
873 return c;
874 }
875
876 // ErrorProp trigonometric functions
877
878 //Sine of an ErrorProp number
879 public static ErrorProp sin(ErrorProp a){
880 ErrorProp b = new ErrorProp();
881 b.value = Math.sin(a.value);
882 b.error = Math.abs(a.error*Math.cos(a.value));
883 return b;
884 }
885
886 //Cosine of an ErrorProp number
887 public static ErrorProp cos(ErrorProp a){
888 ErrorProp b = new ErrorProp();
889 b.value = Math.cos(a.value);
890 b.error = Math.abs(a.error*Math.sin(a.value));
891 return b;
892 }
893
894 //Tangent of an ErrorProp number
895 public static ErrorProp tan(ErrorProp a){
896 ErrorProp b = new ErrorProp();
897 b.value = Math.tan(a.value);
898 b.error = Math.abs(a.error*Fmath.square(Fmath.sec(a.value)));
899 return b;
900 }
901
902 //Hyperbolic sine of a ErrorProp number
903 public static ErrorProp sinh(ErrorProp a){
904 ErrorProp b = new ErrorProp();
905 b.value = Fmath.sinh(a.value);
906 b.error = Math.abs(a.error*Fmath.cosh(a.value));
907 return b;
908 }
909
910 //Hyperbolic cosine of a ErrorProp number
911 public static ErrorProp cosh(ErrorProp a){
912 ErrorProp b = new ErrorProp();
913 b.value = Fmath.cosh(a.value);
914 b.error = Math.abs(a.error*Fmath.sinh(a.value));
915 return b;
916 }
917
918 //Hyperbolic tangent of a ErrorProp number
919 public static ErrorProp tanh(ErrorProp a){
920 ErrorProp b = new ErrorProp();
921 b.value = Fmath.tanh(a.value);
922 b.error = Math.abs(a.error*Fmath.square(Fmath.sech(a.value)));
923 return b;
924 }
925
926 //Inverse sine of a ErrorProp number
927 public static ErrorProp asin(ErrorProp a){
928 ErrorProp b = new ErrorProp();
929 b.value = Math.asin(a.value);
930 b.error = Math.abs(a.error/Math.sqrt(1.0D - a.value*a.value));
931 return b;
932 }
933
934 //Inverse cosine of a ErrorProp number
935 public static ErrorProp acos(ErrorProp a){
936 ErrorProp b = new ErrorProp();
937 b.value = Math.acos(a.value);
938 b.error = Math.abs(a.error/Math.sqrt(1.0D - a.value*a.value));
939 return b;
940 }
941
942 //Inverse tangent of a ErrorProp number
943 public static ErrorProp atan(ErrorProp a){
944 ErrorProp b = new ErrorProp();
945 b.value = Math.atan(a.value);
946 b.error = Math.abs(a.error/(1.0D + a.value*a.value));
947 return b;
948 }
949
950 //Inverse tangent (atan2) of a ErrorProp number - no correlation
951 public static ErrorProp atan2(ErrorProp a, ErrorProp b){
952 ErrorProp c = new ErrorProp();
953 ErrorProp d = a.over(b);
954 c.value = Math.atan2(a.value, b.value);
955 c.error = Math.abs(d.error/(1.0D + d.value*d.value));
956 return c;
957 }
958 //Inverse tangent (atan2) of a ErrorProp number - correlation
959 public static ErrorProp atan2(ErrorProp a, ErrorProp b, double rho){
960 ErrorProp c = new ErrorProp();
961 ErrorProp d = a.over(b, rho);
962 c.value = Math.atan2(a.value, b.value);
963 c.error = Math.abs(d.error/(1.0D + d.value*d.value));
964 return c;
965 }
966
967 //Inverse hyperbolic sine of a ErrorProp number
968 public static ErrorProp asinh(ErrorProp a){
969 ErrorProp b = new ErrorProp();
970 b.value = Fmath.asinh(a.value);
971 b.error = Math.abs(a.error/Math.sqrt(a.value*a.value + 1.0D));
972 return b;
973 }
974
975 //Inverse hyperbolic cosine of a ErrorProp number
976 public static ErrorProp acosh(ErrorProp a){
977 ErrorProp b = new ErrorProp();
978 b.value = Fmath.acosh(a.value);
979 b.error = Math.abs(a.error/Math.sqrt(a.value*a.value - 1.0D));
980 return b;
981 }
982
983 //Inverse hyperbolic tangent of a ErrorProp number
984 public static ErrorProp atanh(ErrorProp a){
985 ErrorProp b = new ErrorProp();
986 b.value = Fmath.atanh(a.value);
987 b.error = Math.abs(a.error/(1.0D - a.value*a.value));
988 return b;
989 }
990
991 // SOME USEFUL NUMBERS
992 // returns the number zero (0) with zero error
993 public static ErrorProp zero(){
994 ErrorProp c = new ErrorProp();
995 c.value=0.0D;
996 c.error=0.0D;
997 return c;
998 }
999
1000 // returns the number one (+1) with zero error
1001 public static ErrorProp plusOne(){
1002 ErrorProp c = new ErrorProp();
1003 c.value=1.0D;
1004 c.error=0.0D;
1005 return c;
1006 }
1007
1008 // returns the number minus one (-1) with zero error
1009 public static ErrorProp minusOne(){
1010 ErrorProp c = new ErrorProp();
1011 c.value=-1.0D;
1012 c.error=0.0D;
1013 return c;
1014 }
1015
1016 // Private methods
1017 // Safe calculation of sqrt(a*a + b*b + 2*r*a*b)
1018 private static double hypotWithCov(double a, double b, double r){
1019 double pre=0.0D, ratio=0.0D, sgn=0.0D;
1020
1021 if(a==0.0D && b==0.0D)return 0.0D;
1022 if(Math.abs(a)>Math.abs(b)){
1023 pre = Math.abs(a);
1024 ratio = b/a;
1025 sgn = Fmath.sign(a);
1026 }
1027 else{
1028 pre = Math.abs(b);
1029 ratio = a/b;
1030 sgn = Fmath.sign(b);
1031 }
1032 return pre*Math.sqrt(1.0D + ratio*(ratio + 2.0D*r*sgn));
1033 }
1034}
1035
Note: See TracBrowser for help on using the repository browser.