Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,48 @@
|
|||
generic
|
||||
type Real is digits <>;
|
||||
with function Sqrt(X: Real) return Real;
|
||||
with function "**"(X: Real; Y: Real) return Real;
|
||||
package Approximation is
|
||||
|
||||
type Number is private;
|
||||
|
||||
-- create an approximation
|
||||
function Approx(Value: Real; Sigma: Real) return Number;
|
||||
|
||||
-- unary operations and conversion Real to Number
|
||||
function "+"(X: Real) return Number;
|
||||
function "-"(X: Real) return Number;
|
||||
function "+"(X: Number) return Number;
|
||||
function "-"(X: Number) return Number;
|
||||
|
||||
-- addition / subtraction
|
||||
function "+"(X: Number; Y: Number) return Number;
|
||||
function "-"(X: Number; Y: Number) return Number;
|
||||
|
||||
-- multiplication / division
|
||||
function "*"(X: Number; Y: Number) return Number;
|
||||
function "/"(X: Number; Y: Number) return Number;
|
||||
|
||||
-- exponentiation
|
||||
function "**"(X: Number; Y: Positive) return Number;
|
||||
function "**"(X: Number; Y: Real) return Number;
|
||||
|
||||
-- Output to Standard IO (wrapper for Ada.Text_IO and Ada.Text_IO.Float_IO)
|
||||
procedure Put_Line(Message: String;
|
||||
Item: Number;
|
||||
Value_Fore: Natural := 7;
|
||||
Sigma_Fore: Natural := 4;
|
||||
Aft: Natural := 2;
|
||||
Exp: Natural := 0);
|
||||
procedure Put(Item: Number;
|
||||
Value_Fore: Natural := 7;
|
||||
Sigma_Fore: Natural := 3;
|
||||
Aft: Natural := 2;
|
||||
Exp: Natural := 0);
|
||||
|
||||
private
|
||||
type Number is record
|
||||
Value: Real;
|
||||
Sigma: Real;
|
||||
end record;
|
||||
end Approximation;
|
||||
|
|
@ -0,0 +1,119 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
package body Approximation is
|
||||
|
||||
package RIO is new Ada.Text_IO.Float_IO(Real);
|
||||
|
||||
-- create an approximation
|
||||
|
||||
function Approx(Value: Real; Sigma: Real) return Number is
|
||||
begin
|
||||
return (Value, Sigma);
|
||||
end Approx;
|
||||
|
||||
-- unary operations and conversion Real to Number
|
||||
|
||||
function "+"(X: Real) return Number is
|
||||
begin
|
||||
return Approx(X, 0.0);
|
||||
end "+";
|
||||
|
||||
function "-"(X: Real) return Number is
|
||||
begin
|
||||
return Approx(-X, 0.0);
|
||||
end "-";
|
||||
|
||||
function "+"(X: Number) return Number is
|
||||
begin
|
||||
return X;
|
||||
end "+";
|
||||
|
||||
function "-"(X: Number) return Number is
|
||||
begin
|
||||
return Approx(-X.Value, X.Sigma);
|
||||
end "-";
|
||||
|
||||
-- addition / subtraction
|
||||
|
||||
function "+"(X: Number; Y: Number) return Number is
|
||||
Z: Number;
|
||||
begin
|
||||
Z.Value := X.Value + Y.Value;
|
||||
Z.Sigma := Sqrt(X.Sigma*X.Sigma + Y.Sigma*Y.Sigma);
|
||||
return Z;
|
||||
end "+";
|
||||
|
||||
function "-"(X: Number; Y: Number) return Number is
|
||||
begin
|
||||
return X + (-Y);
|
||||
end "-";
|
||||
|
||||
-- multiplication / division
|
||||
|
||||
function "*"(X: Number; Y: Number) return Number is
|
||||
Z: Number;
|
||||
begin
|
||||
Z.Value := X.Value * Y.Value;
|
||||
Z.Sigma := Z.Value * Sqrt((X.Sigma/X.Value)**2 + (Y.Sigma/Y.Value)**2);
|
||||
return Z;
|
||||
end "*";
|
||||
|
||||
function "/"(X: Number; Y: Number) return Number is
|
||||
Z: Number;
|
||||
begin
|
||||
Z.Value := X.Value / Y.Value;
|
||||
Z.Sigma := Z.Value * Sqrt((X.Sigma/X.Value)**2 + (Y.Sigma/Y.Value)**2);
|
||||
return Z;
|
||||
end "/";
|
||||
|
||||
-- exponentiation
|
||||
|
||||
function "**"(X: Number; Y: Positive) return Number is
|
||||
Z: Number;
|
||||
begin
|
||||
Z.Value := X.Value ** Y ;
|
||||
Z.Sigma := Z.Value * Real(Y) * (X.Sigma/X.Value);
|
||||
if Z.Sigma < 0.0 then
|
||||
Z.Sigma := - Z.Sigma;
|
||||
end if;
|
||||
return Z;
|
||||
end "**";
|
||||
|
||||
function "**"(X: Number; Y: Real) return Number is
|
||||
Z: Number;
|
||||
begin
|
||||
Z.Value := X.Value ** Y ;
|
||||
Z.Sigma := Z.Value * Y * (X.Sigma/X.Value);
|
||||
if Z.Sigma < 0.0 then
|
||||
Z.Sigma := - Z.Sigma;
|
||||
end if;
|
||||
return Z;
|
||||
end "**";
|
||||
|
||||
-- Output to Standard IO (wrapper for Ada.Text_IO.Float_IO)
|
||||
|
||||
procedure Put_Line(Message: String;
|
||||
Item: Number;
|
||||
Value_Fore: Natural := 7;
|
||||
Sigma_Fore: Natural := 4;
|
||||
Aft: Natural := 2;
|
||||
Exp: Natural := 0) is
|
||||
begin
|
||||
Ada.Text_IO.Put(Message);
|
||||
Put(Item, Value_Fore, Sigma_Fore, Aft, Exp);
|
||||
Ada.Text_IO.New_Line;
|
||||
end Put_Line;
|
||||
|
||||
procedure Put(Item: Number;
|
||||
Value_Fore: Natural := 7;
|
||||
Sigma_Fore: Natural := 3;
|
||||
Aft: Natural := 2;
|
||||
Exp: Natural := 0) is
|
||||
begin
|
||||
RIO.Put(Item.Value, Value_Fore, Aft, Exp);
|
||||
Ada.Text_IO.Put(" (+-");
|
||||
RIO.Put(Item.Sigma, Sigma_Fore, Aft, Exp);
|
||||
Ada.Text_IO.Put(")");
|
||||
end Put;
|
||||
|
||||
end Approximation;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
with Approximation, Ada.Numerics.Elementary_Functions;
|
||||
|
||||
procedure Test_Approximations is
|
||||
package A is new Approximation(Float,
|
||||
Ada.Numerics.Elementary_Functions.Sqrt,
|
||||
Ada.Numerics.Elementary_Functions."**");
|
||||
use type A.Number;
|
||||
X1: A.Number := A.Approx(100.0, 1.1);
|
||||
Y1: A.Number := A.Approx( 50.0, 1.2);
|
||||
X2: A.Number := A.Approx(200.0, 2.2);
|
||||
Y2: A.Number := A.Approx(100.0, 2.3);
|
||||
|
||||
begin
|
||||
A.Put_Line("Distance:",
|
||||
((X1-X2)**2 + (Y1 - Y2)**2)**0.5,
|
||||
Sigma_Fore => 1);
|
||||
end Test_Approximations;
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
class Uncertain {
|
||||
constructor(num, err) {
|
||||
this.num = num;
|
||||
this.err = err;
|
||||
}
|
||||
|
||||
add(x) {
|
||||
try {
|
||||
const res = new Uncertain(this.num + x.num, Math.hypot(this.err, x.err));
|
||||
return res;
|
||||
} catch {
|
||||
const res = new Uncertain(this.num + x, this.err);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
sub(x) {
|
||||
try {
|
||||
const res = new Uncertain(this.num - x.num, Math.hypot(this.err, x.err));
|
||||
return res;
|
||||
} catch {
|
||||
const res = new Uncertain(this.num - x, this.err);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
mul(x) {
|
||||
try {
|
||||
const f = this.num * x.num;
|
||||
const sq = Math.hypot(this.err / this.num, x.err / x.num);
|
||||
const res = new Uncertain(f, f * sq);
|
||||
return res;
|
||||
} catch {
|
||||
const res = new Uncertain(this.num * x, Math.abs(this.err * x));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
div(x) {
|
||||
try {
|
||||
const f = this.num / x.num;
|
||||
const sq = Math.hypot(this.err / this.num, x.err / x.num);
|
||||
const res = new Uncertain(f, f * sq);
|
||||
return res;
|
||||
} catch {
|
||||
const res = new Uncertain(this.num / x, Math.abs(this.err / x));
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
pow(x) {
|
||||
const f = this.num ** x;
|
||||
const res = new Uncertain(f, Math.abs(f * x * this.err / this.num));
|
||||
return res;
|
||||
}
|
||||
|
||||
print() {
|
||||
console.log(`${this.num} +/- ${this.err}`);
|
||||
}
|
||||
}
|
||||
|
||||
const x1 = new Uncertain(100, 1.1);
|
||||
const y1 = new Uncertain(50, 1.2);
|
||||
const x2 = new Uncertain(200, 2.2);
|
||||
const y2 = new Uncertain(100, 2.3);
|
||||
|
||||
const d = x1.sub(x2).pow(2).add(y1.sub(y2).pow(2)).pow(0.5);
|
||||
d.print();
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
class approx
|
||||
static function from(a)
|
||||
if a instanceof approx then return new approx(a.nu, a.sigma) end
|
||||
if type(a) == "number" then return new approx(a, 0) end
|
||||
end
|
||||
|
||||
function __construct(public nu, public sigma) end
|
||||
|
||||
function __add(a)
|
||||
if a instanceof approx then
|
||||
local sr = math.sqrt(self.sigma * self.sigma + a.sigma * a.sigma)
|
||||
return new approx(self.nu + a.nu, sr)
|
||||
end
|
||||
if type(a) == "number" then return new approx(self.nu + a, self.sigma) end
|
||||
end
|
||||
|
||||
function __sub(a)
|
||||
if a instanceof approx then
|
||||
local sr = math.sqrt(self.sigma * self.sigma + a.sigma * a.sigma)
|
||||
return new approx(self.nu - a.nu, sr)
|
||||
end
|
||||
if type(a) == "number" then return new approx(self.nu - a, self.sigma) end
|
||||
end
|
||||
|
||||
function __mul(a)
|
||||
if a instanceof approx then
|
||||
local v = self.nu * a.nu
|
||||
local sq = v * v * self.sigma * self.sigma / (self.nu * self.nu)
|
||||
sq += a.sigma * a.sigma / (a.nu * a.nu)
|
||||
return new approx(v, math.sqrt(sq))
|
||||
end
|
||||
if type(a) == "number" then
|
||||
return new approx(self.nu * a, math.abs(a * self.sigma))
|
||||
end
|
||||
end
|
||||
|
||||
function __div(a)
|
||||
if a instanceof approx then
|
||||
local v = self.nu / a.nu
|
||||
local sq = v * v * self.sigma * self.sigma / (self.nu * self.nu)
|
||||
sq += a.sigma * a.sigma / (a.nu * a.nu)
|
||||
return new approx(v, math.sqrt(sq))
|
||||
end
|
||||
if type(a) == "number" then
|
||||
return new approx(self.nu / a, math.abs(a * self.sigma))
|
||||
end
|
||||
end
|
||||
|
||||
function __pow(d)
|
||||
local v = self.nu ^ d
|
||||
return new approx(v, math.abs(v * d * self.sigma / self.nu))
|
||||
end
|
||||
|
||||
function __tostring() return $"{self.nu} ±{self.sigma}" end
|
||||
end
|
||||
|
||||
local x1 = new approx(100, 1.1)
|
||||
local y1 = new approx( 50, 1.2)
|
||||
local x2 = new approx(200, 2.2)
|
||||
local y2 = new approx(100, 2.3)
|
||||
print(((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ 0.5)
|
||||
61
Task/Numeric-error-propagation/R/numeric-error-propagation.r
Normal file
61
Task/Numeric-error-propagation/R/numeric-error-propagation.r
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
#Plus-minus operator to generate uncertain numbers
|
||||
`%+-%` <- function(x, sigma){
|
||||
if(!(is.numeric(x) & is.numeric(sigma))) stop("both arguments must be numeric")
|
||||
structure(list("num"=x, "err"=sigma), class="uncertain")
|
||||
}
|
||||
|
||||
#Coercing floats (or integers) to uncertain numbers (not used here)
|
||||
as.uncertain <- function(x) x%+-%0
|
||||
|
||||
#Operators for uncertain numbers
|
||||
`+.uncertain` <- function(a, b){
|
||||
if(isa(a, "uncertain") & isa(b, "uncertain")){
|
||||
(a$num+b$num)%+-%sqrt(a$err^2+b$err^2)
|
||||
}
|
||||
else if(is.numeric(a)) b+a%+-%0
|
||||
else if(is.numeric(b)) a+b%+-%0
|
||||
else stop("non-numeric argument")
|
||||
}
|
||||
|
||||
`-.uncertain` <- function(a, b){
|
||||
if(isa(a, "uncertain") & isa(b, "uncertain")){
|
||||
(a$num-b$num)%+-%sqrt(a$err^2+b$err^2)
|
||||
}
|
||||
else if(is.numeric(a)) b-a%+-%0
|
||||
else if(is.numeric(b)) a-b%+-%0
|
||||
else stop("non-numeric argument")
|
||||
}
|
||||
|
||||
`*.uncertain` <- function(a, b){
|
||||
if(isa(a, "uncertain") & isa(b, "uncertain")){
|
||||
(a$num*b$num)%+-%a$num*b$num*sqrt((a$err/a$num)^2+(b$err/b$num)^2)
|
||||
}
|
||||
else if(is.numeric(a)) (b$num*a)%+-%abs(a*b$err)
|
||||
else if(is.numeric(b)) (a$num*b)%+-%abs(b*a$err)
|
||||
else stop("non-numeric argument")
|
||||
}
|
||||
|
||||
`/.uncertain` <- function(a, b){
|
||||
if(isa(a, "uncertain") & isa(b, "uncertain")){
|
||||
(a$num/b$num)%+-%a$num*b$num*sqrt((a$err/a$num)^2+(b$err/b$num)^2)
|
||||
}
|
||||
else if(is.numeric(a)) (b$num/a)%+-%abs(b$err/a)
|
||||
else if(is.numeric(b)) (a$num/b)%+-%abs(a$err/b)
|
||||
else stop("non-numeric argument")
|
||||
}
|
||||
|
||||
`^.uncertain` <- function(a,b){
|
||||
if(!is.numeric(b)) stop("exponent must be integer or double")
|
||||
(a$num^b)%+-%abs(b*a$err*a$num^(b-1))
|
||||
}
|
||||
|
||||
#We need a print method to actually display uncertain numbers
|
||||
print.uncertain <- function(x) cat(x$num, "+/-", x$err)
|
||||
|
||||
#The calculation
|
||||
x1 <- 100%+-%1.1
|
||||
y1 <- 50%+-%1.2
|
||||
x2 <- 200%+-%2.2
|
||||
y2 <- 100%+-%2.3
|
||||
|
||||
d <- print(((x1-x2)^2+(y1-y2)^2)^(1/2))
|
||||
Loading…
Add table
Add a link
Reference in a new issue