1
2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3// Copyright (C) ????-2008 - INRIA
4//
5// Copyright (C) 2012 - 2016 - Scilab Enterprises
6//
7// This file is hereby licensed under the terms of the GNU GPL v2.0,
8// pursuant to article 5.3.4 of the CeCILL v.2.1.
9// This file was originally licensed under the terms of the CeCILL v2.1,
10// and continues to be available under such terms.
11// For more information, see the COPYING file which you should have received
12// along with this program.
13
14function t=trace(a)
15    // trace - computes the trace of a matrix
16    select type(a)
17    case 1 then
18        [m,n]=size(a)
19        if m<>n then
20            error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"trace",1));
21        end
22        t=sum(diag(a))
23    case 2 then
24        [m,n]=size(a)
25        if m<>n then
26            error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"trace",1));
27        end
28        t=sum(diag(a))
29        //-compat next case retained for list /tlist compatibility
30    case 15 then
31        if a(1)=="r" then
32            [m,n]=size(a)
33            if m<>n then
34                error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"trace",1));
35            end
36            t=sum(diag(a))
37        else
38            error(msprintf(gettext("%s: Wrong type for input argument #%d.\n"),"trace",1));
39        end
40    case 16 then
41        if a(1)=="r" then
42            [m,n]=size(a)
43            if m<>n then
44                error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"trace",1));
45            end
46            t=sum(diag(a))
47        else
48            error(msprintf(gettext("%s: Wrong type for input argument #%d.\n"),"trace",1));
49        end
50    case 5 then
51        [m,n]=size(a)
52        if m<>n then
53            error(msprintf(gettext("%s: Wrong size for input argument #%d: A square matrix expected.\n"),"trace",1));
54        end
55        t=sum(diag(a))
56    else
57        error(msprintf(gettext("%s: Wrong type for input argument #%d.\n"),"trace",1));
58    end
59endfunction
60