File: nmatrix2.cpp

package info (click to toggle)
regina-normal 4.3.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 16,396 kB
  • ctags: 7,338
  • sloc: cpp: 55,359; sh: 17,392; ansic: 12,913; perl: 3,294; makefile: 918; python: 114
file content (137 lines) | stat: -rw-r--r-- 5,185 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

/**************************************************************************
 *                                                                        *
 *  Regina - A Normal Surface Theory Calculator                           *
 *  Python Interface                                                      *
 *                                                                        *
 *  Copyright (c) 1999-2006, Ben Burton                                   *
 *  For further details contact Ben Burton (bab@debian.org).              *
 *                                                                        *
 *  This program is free software; you can redistribute it and/or         *
 *  modify it under the terms of the GNU General Public License as        *
 *  published by the Free Software Foundation; either version 2 of the    *
 *  License, or (at your option) any later version.                       *
 *                                                                        *
 *  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 GNU     *
 *  General Public License for more details.                              *
 *                                                                        *
 *  You should have received a copy of the GNU General Public             *
 *  License along with this program; if not, write to the Free            *
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,       *
 *  MA 02110-1301, USA.                                                   *
 *                                                                        *
 **************************************************************************/

/* end stub */

#include "utilities/nmatrix2.h"
#include <boost/python.hpp>

namespace regina {
    /**
     * A utility class that provides access to a single row of a 2-by-2
     * matrix.  This allows us to write m[i][j] in Python for a 2-by-2
     * matrix m.
     */
    class NMatrix2Row {
        private:
            long* row;

        public:
            NMatrix2Row(NMatrix2& matrix, int whichRow) :
                    row(matrix[whichRow]) {
            }

            long getItem(int whichCol) {
                if (whichCol < 0 || whichCol > 1) {
                    PyErr_SetString(PyExc_IndexError,
                        "NMatrix2 column index out of range");
                    ::boost::python::throw_error_already_set();
                }
                return row[whichCol];
            }

            void setItem(int whichCol, long value) {
                if (whichCol < 0 || whichCol > 1) {
                    PyErr_SetString(PyExc_IndexError,
                        "NMatrix2 column index out of range");
                    ::boost::python::throw_error_already_set();
                }
                row[whichCol] = value;
            }
    };
}

namespace {
    /**
     * A trivial routine for returning the size of a 2-by-2 matrix.
     */
    template <class T>
    long size2(const T&) {
        return 2;
    }

    /**
     * Return a new object for accessing the given row of the given matrix.
     */
    regina::NMatrix2Row* NMatrix2_getRow(regina::NMatrix2& m, int whichRow) {
        if (whichRow < 0 || whichRow > 1) {
            PyErr_SetString(PyExc_IndexError,
                "NMatrix2 row index out of range");
            ::boost::python::throw_error_already_set();
        }
        return new regina::NMatrix2Row(m, whichRow);
    }
}

using namespace boost::python;
using regina::NMatrix2;
using regina::NMatrix2Row;

namespace {
    bool (*simpler1)(const NMatrix2&, const NMatrix2&) = &regina::simpler;
    bool (*simpler2)(const NMatrix2&, const NMatrix2&,
        const NMatrix2&, const NMatrix2&) = &regina::simpler;
}

void addNMatrix2() {
    class_<NMatrix2Row>("NMatrix2Row", ::boost::python::no_init)
        .def("__getitem__", &NMatrix2Row::getItem)
        .def("__setitem__", &NMatrix2Row::setItem)
        .def("__len__", size2<NMatrix2Row>)
        ;

    class_<NMatrix2>("NMatrix2")
        .def(init<const NMatrix2&>())
        .def(init<long, long, long, long>())
        .def("__getitem__", NMatrix2_getRow,
            return_value_policy<manage_new_object,
            with_custodian_and_ward_postcall<0, 1> >())
        .def("__len__", &size2<regina::NMatrix2>)
        .def(self * self)
        .def(self * long())
        .def(self + self)
        .def(self - self)
        .def(- self)
        .def("transpose", &NMatrix2::transpose)
        .def("inverse", &NMatrix2::inverse)
        .def(self += self)
        .def(self -= self)
        .def(self *= self)
        .def(self *= long())
        .def("negate", &NMatrix2::negate)
        .def("invert", &NMatrix2::invert)
        .def(self == self)
        .def(self != self)
        .def("determinant", &NMatrix2::determinant)
        .def("isIdentity", &NMatrix2::isIdentity)
        .def("isZero", &NMatrix2::isZero)
        .def(self_ns::str(self))
    ;

    def("simpler", simpler1);
    def("simpler", simpler2);
}