summaryrefslogtreecommitdiff
path: root/modules/m2sci/macros/percent/%a2sci.sci
blob: 8c503ddd04b61a187dd0a4264744077effc961b0 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [tree]=%a2sci(tree)
    // M2SCI function
    // Conversion function for Matlab addition
    // Input: tree = Matlab operation tree
    // Output: tree = Scilab equivalent for tree
    // Emulation function: mtlb_a()

    // In Matlab only two matrices with the same size can be added unless one is a scalar
    // So empty matrix can only be added to a scalar or an onther empty matrix
    // For example : [1,2,3]+[] is not possible
    // An other important difference with Scilab is that :
    //  - Matlab gives : [1]+[]=[]
    //  - Scilab gives : [1]+[]=[1]

    // WARNING : translation does not work for codes like var=+'a'
    // In this case, user have to modify M-file and replace +'a' by 0+'a' for example

    // Overloading functions in $SCI/modules/compatibility_functions/macros/:
    // - %b_a_s.sci
    // - %s_a_b.sci

    // Binary operator: A+B
    if size(tree.operands)==2 then
        [A,B]=getoperands(tree)

        // Matlab and Scilab addition do not match for Strings
        if or(A.vtype==[String,Unknown]) then
            A=convert2double(A)
        end
        if or(B.vtype==[String,Unknown]) then
            B=convert2double(B)
        end

        // %b_a_b is not defined in Scilab
        if A.vtype==Boolean & B.vtype==Boolean
            A=convert2double(A)
        end

        tree.operands=list(A,B)

        // Type inference
        if is_real(A) & is_real(B) then
            tree.out(1).type=Type(Double,Real)
        else
            tree.out(1).type=Type(Double,Unknown)
        end

        // When both operands are not [] Scilab and Matlab give the same results
        if not_empty(A) & not_empty(B) then
            if is_a_scalar(A) then
                tree.out(1).dims=B.dims
            else
                tree.out(1).dims=A.dims
            end
            // If at least one operand is [] then Matlab result is [] but not Scilab one
        elseif is_empty(A) | is_empty(B) then
            set_infos(msprintf(gettext("At least one operand of %s is an empty matrix, Scilab equivalent is []."),expression2code(tree)),0)
            tree=Cste([])
        else
            tree.out(1).dims=allunknown(A.dims)
            tree=Funcall("mtlb_a",1,list(A,B),tree.out)
        end
    else
        // This case can not exist because Scilab interpreter ignores unary +
        A=getoperands(tree)
        //A=convert2double(A)
        tree.operands=list(A)
        tree.out(1).infer=A.infer
    end
endfunction