1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) INRIA
3//
4// Copyright (C) 2012 - 2016 - Scilab Enterprises
5//
6// This file is hereby licensed under the terms of the GNU GPL v2.0,
7// pursuant to article 5.3.4 of the CeCILL v.2.1.
8// This file was originally licensed under the terms of the CeCILL v2.1,
9// and continues to be available under such terms.
10// For more information, see the COPYING file which you should have received
11// along with this program.
12
13function f = %r_a_r(s1, s2)
14    if ndims(s1)<2 & ndims(s2)<2 then
15        [s1,s2] = sysconv(s1,s2)
16    end
17    [num1,den1] = s1(["num","den"])
18    [num2,den2] = s2(["num","den"])
19    sz1 = size(num1)
20    sz2 = size(num2)
21
22    if and(sz1>=0)&and(sz2>=0) then
23        num1 = num1(:)
24        den1 = den1(:)
25        num2 = num2(:)
26        den2 = den2(:)
27
28        if prod(sz1)==1 & prod(sz2)>1 then
29            den1 = den1(ones(den2))
30            num1 = num1(ones(num2))
31            sz1 = sz2
32        elseif prod(sz2)==1 & prod(sz1)>1 then
33            den2 = den2(ones(den1))
34            num2 = num2(ones(num1))
35            sz2 = sz1
36        end
37        if and(sz1<>sz2) then
38            msg = _("%s: Inconsistent addition.\n")
39            error(msprintf(msg, "%r_a_r"))
40        end
41        for l = 1:prod(sz1)
42            [den,fact] = lcm([den1(l); den2(l)])
43            num1(l) = [num1(l),num2(l)]*fact
44            den1(l) = den
45        end
46        [num1,den1] = simp(num1,den1)
47        f = rlist(matrix(num1,sz1),matrix(den1,sz1),s1.dt)
48    else
49        if size(sz1,"*")>2|size(sz2,"*")>2 then
50            msg = _("%s: Inconsistent addition.\n")
51            error(msprintf(msg, "%r_a_r"))
52        end
53        if or(sz1<0) & or(sz2<0) then
54            // both are eye*x
55            [den1,fact] = lcm([den1; den2])
56            [num1,den1] = simp([num1, num2]*fact, den1)
57            f = rlist(num1*eye(),den1*eye(),s1("dt"))
58        elseif or(sz1<0) then
59            den1 = den1+0
60            num1 = num1+0
61            for l = 1:min(sz2)
62                [den,fact] = lcm([den1; den2(l,l)])
63                num2(l,l) = [num1, num2(l,l)]*fact
64                den2(l,l) = den
65            end
66            [num2,den2] = simp(num2, den2),
67            f = rlist(num2, den2, s1.dt)
68        elseif or(sz2<0) then
69            den2 = den2+0
70            num2 = num2+0
71            for l = 1:min(sz1)
72                [den,fact] = lcm([den1(l,l); den2])
73                num1(l,l) = [num1(l,l) num2]*fact
74                den1(l,l) = den
75            end
76            [num1,den1] = simp(num1,den1)
77            f = rlist(num1, den1, s1.dt)
78        end
79    end
80    if simp_mode()
81        f = simp(f)
82    end
83endfunction
84