How do I implement a numeric data type?
Show older comments
Hello MatLab experts,
I'm implementing a numeric data type. It must (eventually) implement all of the mathematical operators and functions +, *, -, /, log, exp, etc...
The problem I'm running into is that objects in matlab can implicitly be stored as vectors or matrices, and I'm having trouble figuring out how best to deal with that. This project is currently in-progress and not very well organized, but the full file is currently available here https://github.com/jeffersoncarpenter/RandomVariable/blob/master/RandomVariable.m
The numeric data type is for "random variables" or specifically for observed values with known uncertainties that I'm collecting as physics lab data.
There is a formula for error propagation involving the squares of partial derivatives, and it amounts to applying the chain rule at each step of the computation in which the numbers are pairs (observational value, standard deviation) and add, multiply, and so on as random variables. Ergo the "random variable" numeric data type.
Here is a sample class method. It's pretty long, but the thing to notice is the top-level if statement checking whether the argument is a Datum (random variable) or a standard number, followed by more if statements to determine whether to loop over the left-hand argument, the right-hand argument, or both. Ultimately I will have to support multiplying on the left by standard numbers, multiplying on the right by standard numbers, and multiplying two random variables together, together with every combination of vector or matrix rank.
Is there a better way to do this? I expect to have to branch to check whether I'm operating on the left-hand side or the right-hand side with a standard number instead of a Datum (random variable), but I would like to not have to further check - in the body of each operator, that would be a lot of code - whether the left-hand side, right-hand side, or both are vectors or matrices. It would be great if that could be written more compactly.
function r = mtimes(a, b)
if isa(a, 'Datum')
if 1 == numel(b)
r = a;
for i = 1:numel(a)
r(i) = Datum(a(i).Value * b.Value, a(i).Value^2 * b.Variance + a(i).Variance * b.Value^2);
end
elseif (1 == numel(a))
r = b;
for i = 1:numel(b)
r(i) = Datum(a.Value * b(i).Value, a.Value^2 * b(i).Variance + a.Variance * b(i).Value^2);
end
else
r = a;
for i = 1:numel(a)
r(i) = Datum(a(i).Value * b(i).Value, a(i).Value^2 * b(i).Variance + a(i).Variance * b(i).Value^2);
end
end
else
if 1 == numel(b)
if 1 == numel(a)
r = Datum(a * b.Value, a^2 * b.Variance);
else
for i = 1:numel(a)
r(i) = Datum(a(i) * b.Value, a(i)^2 * b.Variance);
end
end
else
r = b;
for i = 1:numel(b)
r(i) = Datum(a * b(i).Value, a^2 * b(i).Variance);
end
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Surfaces, Volumes, and Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!