-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultipler.m
52 lines (48 loc) · 1.63 KB
/
Multipler.m
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
classdef Multipler < handle
%ELE Summary of this class goes here
% Detailed explanation goes here
properties
a ;
b ; % two computational unit to be multiplied
taylor3 ; % taylor series, log(|coe|), coe*k^n/n!,
len ; % taylor3(:,1) store log(|coe|), while taylor3(:,2) store sign(coe)
end
methods
% init is the initial value of comp unit
function newComp = Multipler(a, b)
newComp.a = a;
newComp.b = b;
newComp.taylor3 = [];
newComp.len = 0;
end
% append another term of taylor's series
function [this] = add( this, v , s )
this.len = this.len + 1;
if s == 0
this.taylor3(this.len , 1) = 0;
else
this.taylor3(this.len , 1) = v;
end
this.taylor3(this.len , 2) = s;
end
% compute all relatioins for this term and sum them up
function [this] = compute(this)
o = this.len + 1;
a = this.a.taylor3(1:o , :);
b = flipud( this.b.taylor3(1:o , :) );
ss = a(:,2) .* b(:,2);
vv = a(:,1) + b(:,1);
ave = max(vv) - 50;
vv = vv - ave;
vv = exp(vv) .* ss;
v = sum(vv);
s = sign(v);
v = log(abs(v)) + ave;
this.add( v , s );
end
function [v , s] = lastTermLog(this)
v = this.taylor3(this.len , 1) + multFactor.logfactorial(this.len-1);
s = this.taylor3(this.len , 2);
end
end
end