Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -8,9 +8,7 @@ const struct Imprecise {
|
|||
this.delta = abs(d);
|
||||
}
|
||||
|
||||
template IsImprecise(T) {
|
||||
enum IsImprecise = is(Unqual!T == Unqual!(typeof(this)));
|
||||
}
|
||||
enum IsImprecise(T) = is(Unqual!T == Unqual!(typeof(this)));
|
||||
|
||||
I reciprocal() const pure nothrow {
|
||||
return I(1.0 / value, delta / (value ^^ 2));
|
||||
|
|
@ -83,7 +81,7 @@ const struct Imprecise {
|
|||
}
|
||||
}
|
||||
|
||||
alias Imprecise I;
|
||||
alias I = Imprecise;
|
||||
|
||||
auto distance(T1, T2)(in T1 p1, in T2 p2) pure nothrow {
|
||||
return ((p1[0] - p2[0]) ^^ 2 + (p1[1] - p2[1]) ^^ 2) ^^ 0.5;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
record num(val,err)
|
||||
|
||||
procedure main(a)
|
||||
x1 := num(100.0, 1.1)
|
||||
y1 := num(50.0, 1.2)
|
||||
x2 := num(200.0, 2.2)
|
||||
y2 := num(100.0, 2.3)
|
||||
d := pow(add(pow(sub(x1,x2),2),pow(sub(y1,y2),2)),0.5)
|
||||
write("d = [",d.val,", ",d.err,"]")
|
||||
end
|
||||
|
||||
procedure add(a,b)
|
||||
return (numeric(a)+numeric(b)) |
|
||||
num(numeric(a)+b.val, b.err) |
|
||||
num(a.val+numeric(b), a.err) |
|
||||
num(a.val+b.val, (a.err^2 + b.err^2) ^ 0.5)
|
||||
end
|
||||
|
||||
procedure sub(a,b)
|
||||
return (numeric(a)-numeric(b)) |
|
||||
num(numeric(a)-b.val, b.err) |
|
||||
num(a.val-numeric(b), a.err) |
|
||||
num(a.val-b.val, (a.err^2 + b.err^2) ^ 0.5)
|
||||
end
|
||||
|
||||
procedure mul(a,b)
|
||||
return (numeric(a)*numeric(b)) |
|
||||
num(numeric(a)*b.val, abs(a*b.err)) |
|
||||
num(a.val*numeric(b), abs(b*a.err)) |
|
||||
num(f := a.val*b.val, ((f^2*((a.err/a.val)^2+(b.err/b.val)^2))^0.5))
|
||||
end
|
||||
|
||||
procedure div(a,b)
|
||||
return (numeric(a)/numeric(b)) |
|
||||
num(numeric(a)/b.val, abs(a*b.err)) |
|
||||
num(a.val/numeric(b), abs(b*a.err)) |
|
||||
num(f := a.val/b.val, ((f^2*((a.err/a.val)^2+(b.err/b.val)^2))^0.5))
|
||||
end
|
||||
|
||||
procedure pow(a,b)
|
||||
return (numeric(a)^numeric(b)) |
|
||||
num(f := a.val^numeric(b), abs(f*b*(a.err/a.val)))
|
||||
end
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
public class Approx {
|
||||
private double value;
|
||||
private double error;
|
||||
|
||||
public Approx(){this.value = this.error = 0;}
|
||||
|
||||
public Approx(Approx b){
|
||||
this.value = b.value;
|
||||
this.error = b.error;
|
||||
}
|
||||
|
||||
public Approx(double value, double error){
|
||||
this.value = value;
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public Approx add(Approx b){
|
||||
value+= b.value;
|
||||
error = Math.sqrt(error * error + b.error * b.error);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx add(double b){
|
||||
value+= b;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx sub(Approx b){
|
||||
value-= b.value;
|
||||
error = Math.sqrt(error * error + b.error * b.error);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx sub(double b){
|
||||
value-= b;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx mult(Approx b){
|
||||
double oldVal = value;
|
||||
value*= b.value;
|
||||
error = Math.sqrt(value * value * (error*error) / (oldVal*oldVal) +
|
||||
(b.error*b.error) / (b.value*b.value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx mult(double b){
|
||||
value*= b;
|
||||
error = Math.abs(b * error);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx div(Approx b){
|
||||
double oldVal = value;
|
||||
value/= b.value;
|
||||
error = Math.sqrt(value * value * (error*error) / (oldVal*oldVal) +
|
||||
(b.error*b.error) / (b.value*b.value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx div(double b){
|
||||
value/= b;
|
||||
error = Math.abs(b * error);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Approx pow(double b){
|
||||
double oldVal = value;
|
||||
value = Math.pow(value, b);
|
||||
error = Math.abs(value * b * (error / oldVal));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){return value+"±"+error;}
|
||||
|
||||
public static void main(String[] args){
|
||||
Approx x1 = new Approx(100, 1.1);
|
||||
Approx x2 = new Approx(50, 1.2);
|
||||
Approx y1 = new Approx(200, 2.2);
|
||||
Approx y2 = new Approx(100, 2.3);
|
||||
|
||||
x1.sub(x2).pow(2).add(y1.sub(y2).pow(2)).pow(0.5);
|
||||
|
||||
System.out.println(x1);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
add(a,b)=if(type(a)==type(b), a+b, if(type(a)=="t_VEC",a+[b,0],b+[a,0]));
|
||||
sub(a,b)=if(type(a)==type(b), [a[1]-b[1],a[2]+b[2]], if(type(a)=="t_VEC",a-[b,0],[a,0]-b));
|
||||
mult(a,b)=if(type(a)=="t_VEC", if(type(b)=="t_VEC", [a[1]*b[1], abs(a[1]*b[1])*sqrt(norml2([a[2]/a[1],b[2]/b[1]]))], [b*a[1], abs(b)*a[2]]), [a*b[1], abs(a)*b[2]]);
|
||||
div(a,b)=if(type(b)!="t_VEC", mult(a,1/b), mult(a,[1/b[1],b[2]/b[1]^2]));
|
||||
pow(a,b)=[a[1]^b, abs(a[1]^b*b*a[2]/a[1])];
|
||||
x1=[100,1.1];y1=[50,1.2];x2=[200,2.2];y2=[100,2.3];
|
||||
pow(add(pow(sub(x1,x2),2),pow(sub(y1,y2),2)),.5)
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
#lang racket
|
||||
|
||||
(struct ± (x dx) #:transparent
|
||||
#:methods gen:custom-write
|
||||
[(define (write-proc a port mode) (display (±->string a) port))])
|
||||
|
||||
(define/match (±+ a [b 0])
|
||||
[((± x dx) (± y dy)) (± (+ x y) (norm dx dy))]
|
||||
[((± x dx) c) (± (+ x c) dx)]
|
||||
[(_ (± y dy)) (±+ b a)])
|
||||
|
||||
(define/match (±* a b)
|
||||
[((± x dx) (± y dy)) (± (* x y) (* x y (norm (/ dx x) (/ dy y))))]
|
||||
[((± x dx) c) (± (* x c) (abs (* c dx)))]
|
||||
[(_ (± y dy)) (±* b a)])
|
||||
|
||||
(define/match (±- a [b #f])
|
||||
[(a #f) (±* -1 a)]
|
||||
[(a b) (±+ a (±* -1 b))])
|
||||
|
||||
(define/match (±/ a b)
|
||||
[((± x dx) (± y dy)) (± (/ x y) (/ x y (norm (/ dx x) (/ dy y))))]
|
||||
[((± _ _) c) (±* a (/ 1 c))])
|
||||
|
||||
(define/match (±expt a c)
|
||||
[((± x dx) c) (± (expt x c) (abs (* (expt x c) (/ dx x))))])
|
||||
|
||||
(define/match (norm a b)
|
||||
[((± x dx) (± y dy)) (±expt (±+ (±expt a 2) (±expt b 2)) 0.5)]
|
||||
[(x y) (sqrt (+ (sqr x) (sqr y)))])
|
||||
|
||||
(define/match (±->string x [places 3])
|
||||
[((± x dx) p) (string-join (map (λ (s) (real->decimal-string s p))
|
||||
(list x dx))" ± ")])
|
||||
|
||||
;; Test
|
||||
;;
|
||||
(define x1 (± 100 1.1))
|
||||
(define y1 (± 50 1.2))
|
||||
(define x2 (± 200 2.2))
|
||||
(define y2 (± 100 2.3))
|
||||
(norm (±- x1 x2) (±- y1 y2))
|
||||
Loading…
Add table
Add a link
Reference in a new issue