File: Matrix.i

package info (click to toggle)
quantlib-python 0.2.1.cvs20020322-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,040 kB
  • ctags: 3,245
  • sloc: cpp: 32,997; python: 7,156; makefile: 70
file content (193 lines) | stat: -rw-r--r-- 5,655 bytes parent folder | download
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

/*
 Copyright (C) 2000, 2001, 2002 RiskMap srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it under the
 terms of the QuantLib license.  You should have received a copy of the
 license along with this program; if not, please email ferdinando@ametrano.net
 The license is also available online at http://quantlib.org/html/license.html

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

// $Id: Matrix.i,v 1.15 2002/03/04 17:25:41 lballabio Exp $

#ifndef quantlib_matrix_i
#define quantlib_matrix_i

%include String.i
%include QLArray.i

%{
using QuantLib::Math::Matrix;
typedef QuantLib::Math::Matrix::row_iterator MatrixRow;
using QuantLib::Math::outerProduct;
using QuantLib::Math::transpose;
using QuantLib::Math::matrixSqrt;
%}

class MatrixRow {
  private:
    // access control - no constructor exported
    MatrixRow();
  public:
    ~MatrixRow();
};

%addmethods MatrixRow {
    double __getitem__(int i) {
        return (*self)[i];
    }
    void __setitem__(int i, double x) {
        (*self)[i] = x;
    }
};

// typemap Python list of lists of numbers to Matrix
%typemap(python,in) Matrix (Matrix temp),
                    Matrix & (Matrix temp),
                    const Matrix & (Matrix temp) {
    Matrix* m;
    if (PyTuple_Check($source) || PyList_Check($source)) {
        /* Size */ int rows, cols;
        rows = (PyTuple_Check($source) ?
                PyTuple_Size($source) :
                PyList_Size($source));
        // look ahead
        PyObject* o = PySequence_GetItem($source,0);
        if (PyTuple_Check(o) || PyList_Check(o)) {
            cols = (PyTuple_Check(o) ?
                    PyTuple_Size(o) :
                    PyList_Size(o));
            Py_DECREF(o);
        } else {
            PyErr_SetString(PyExc_TypeError, "Matrix expected");
            Py_DECREF(o);
            return NULL;
        }

        temp = Matrix(rows,cols);
        for (/* Size */ int i=0; i<rows; i++) {
            PyObject* o = PySequence_GetItem($source,i);
            if (PyTuple_Check(o) || PyList_Check(o)) {
                /* Size */ int items = (PyTuple_Check(o) ?
                                        PyTuple_Size(o) :
                                        PyList_Size(o));
                if (items != cols) {
                    PyErr_SetString(PyExc_TypeError, 
                        "Matrix must have equal-length rows");
                    Py_DECREF(o);
                    return NULL;
                }
                for (/* Size */ int j=0; j<cols; j++) {
                    PyObject* d = PySequence_GetItem(o,j);
                    if (d == Py_None) {
                        temp[i][j] = Null<double>();
                        Py_DECREF(d);
                    } else if (PyFloat_Check(d)) {
                        temp[i][j] = PyFloat_AsDouble(d);
                        Py_DECREF(d);
                    } else if (PyInt_Check(d)) {
                        temp[i][j] = double(PyInt_AsLong(d));
                        Py_DECREF(d);
                    } else {
                        PyErr_SetString(PyExc_TypeError,"doubles expected");
                        Py_DECREF(d);
                        Py_DECREF(o);
                        return NULL;
                    }
                }
                Py_DECREF(o);
            } else {
                PyErr_SetString(PyExc_TypeError, "Matrix expected");
                Py_DECREF(o);
                return NULL;
            }
        }
        $target = &temp;
    } else if ((SWIG_ConvertPtr($source,(void **) &m,
                                SWIGTYPE_p_Matrix,0)) != -1) {
        $target = m;
    } else {
        PyErr_SetString(PyExc_TypeError,"Matrix expected");
        return NULL;
    }
};

class Matrix {
  private:
    Matrix(); // redefined below
  public:
    ~Matrix();
    /* Size */ int rows() const;
    /* Size */ int columns() const;
};

%addmethods Matrix {
    Matrix(const Matrix& m) {
        return new Matrix(m);
    }
    MatrixRow __getitem__(int i) {
        return (*self)[i];
    }
    Matrix __add__(const Matrix& m) {
        return *self+m;
    }
    Matrix __iadd__(const Matrix& m) {
        return *self+m;
    }
    Matrix __sub__(const Matrix& m) {
        return *self-m;
    }
    Matrix __isub__(const Matrix& m) {
        return *self-m;
    }
    Matrix __mul__(double x) {
        return *self*x;
    }
    Matrix __imul__(double x) {
        return *self*x;
    }
    Matrix __rmul__(double x) {
        return *self*x;
    }
    Matrix __div__(double x) {
        return *self/x;
    }
    Matrix __idiv__(double x) {
        return *self/x;
    }
    String __str__() {
        String s;
        for (/* Size */ unsigned int j=0; j<self->rows(); j++) {
    	    s += "\n";
            s += QuantLib::DoubleFormatter::toString((*self)[j][0]);
            for (/* Size */ unsigned int i=1; i<self->columns(); i++) {
                s += ",";
                s += QuantLib::DoubleFormatter::toString((*self)[j][i]);
            }
        }
        s += "\n";
        return s;
    }
};

// functions

Matrix transpose(const Matrix& m);
Matrix outerProduct(const Array& v1, const Array& v2);
%inline %{
    Matrix matrixProduct(const Matrix& m1, const Matrix& m2) {
        return m1*m2;
    }
%}
Matrix matrixSqrt(const Matrix& m);



#endif