~ubuntu-branches/ubuntu/trusty/python-igraph/trusty

« back to all changes in this revision

Viewing changes to src/arpackobject.c

  • Committer: Package Import Robot
  • Author(s): TANIGUCHI Takaki
  • Date: 2012-03-17 17:23:55 UTC
  • Revision ID: package-import@ubuntu.com-20120317172355-e9iki37igmxnlq38
Tags: upstream-0.5.4
ImportĀ upstreamĀ versionĀ 0.5.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C -*-  */
 
2
/* 
 
3
   IGraph library.
 
4
   Copyright (C) 2007  Gabor Csardi <csardi@rmki.kfki.hu>
 
5
   MTA RMKI, Konkoly-Thege Miklos st. 29-33, Budapest 1121, Hungary
 
6
   
 
7
   This program is free software; you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation; either version 2 of the License, or
 
10
   (at your option) any later version.
 
11
   
 
12
   This program is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
   
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with this program; if not, write to the Free Software
 
19
   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA 
 
20
   02110-1301 USA
 
21
 
 
22
*/
 
23
 
 
24
#include "arpackobject.h"
 
25
#include "graphobject.h"
 
26
#include "error.h"
 
27
 
 
28
PyObject* igraphmodule_arpack_options_default;
 
29
 
 
30
/**
 
31
 * \ingroup python_interface_arpack
 
32
 * \brief Checks if the object is an ARPACK parameter object
 
33
 */
 
34
int igraphmodule_ARPACKOptions_Check(PyObject *ob) {
 
35
  if (ob) return PyType_IsSubtype(ob->ob_type, &igraphmodule_ARPACKOptionsType);
 
36
  return 0;
 
37
}
 
38
 
 
39
/**
 
40
 * \ingroup python_interface_arpack
 
41
 * \brief Allocates a new ARPACK parameters object
 
42
 */
 
43
PyObject* igraphmodule_ARPACKOptions_new() {
 
44
  igraphmodule_ARPACKOptionsObject* self;
 
45
  self=PyObject_New(igraphmodule_ARPACKOptionsObject,
 
46
    &igraphmodule_ARPACKOptionsType);
 
47
  if (self) {
 
48
    igraph_arpack_options_init(&self->params);
 
49
    igraph_arpack_options_init(&self->params_out);
 
50
  }
 
51
  return (PyObject*)self;
 
52
}
 
53
 
 
54
/**
 
55
 * \ingroup python_interface_arpack
 
56
 * \brief Deallocates a Python representation of a given ARPACK parameters object
 
57
 */
 
58
void igraphmodule_ARPACKOptions_dealloc(
 
59
  igraphmodule_ARPACKOptionsObject* self) {
 
60
  /*igraph_arpack_options_destroy(&self->params);*/
 
61
  PyObject_Del((PyObject*)self);
 
62
}
 
63
 
 
64
/** \ingroup python_interface_arpack
 
65
 * \brief Returns one of the attributes of a given ARPACK parameters object
 
66
 */
 
67
PyObject* igraphmodule_ARPACKOptions_getattr(
 
68
  igraphmodule_ARPACKOptionsObject* self, char* attrname) {
 
69
  PyObject *result = NULL;
 
70
 
 
71
  if (strcmp(attrname, "bmat") == 0) {
 
72
    char buf[2] = { self->params_out.bmat[0], 0 };
 
73
    result=PyString_FromString(buf);
 
74
  } else if (strcmp(attrname, "n") == 0) {
 
75
    result=PyInt_FromLong(self->params_out.n);
 
76
  } else if (strcmp(attrname, "which") == 0) {
 
77
    char buf[3] = { self->params.which[0], self->params.which[1], 0 };
 
78
    result=PyString_FromString(buf);
 
79
  } else if (strcmp(attrname, "nev") == 0) {
 
80
    result=PyInt_FromLong(self->params.nev);
 
81
  } else if (strcmp(attrname, "tol") == 0) {
 
82
    result=PyFloat_FromDouble((double)self->params.tol);
 
83
  } else if (strcmp(attrname, "ncv") == 0) {
 
84
    result=PyInt_FromLong(self->params.ncv);
 
85
  } else if (strcmp(attrname, "ldv") == 0) {
 
86
    result=PyInt_FromLong(self->params.ldv);
 
87
  } else if (strcmp(attrname, "ishift") == 0) {
 
88
    result=PyInt_FromLong(self->params.ishift);
 
89
  } else if (strcmp(attrname, "maxiter") == 0 ||
 
90
                     strcmp(attrname, "mxiter") == 0) {
 
91
    result=PyInt_FromLong(self->params.mxiter);
 
92
  } else if (strcmp(attrname, "nb") == 0) {
 
93
    result=PyInt_FromLong(self->params.nb);
 
94
  } else if (strcmp(attrname, "mode") == 0) {
 
95
    result=PyInt_FromLong(self->params.mode);
 
96
  } else if (strcmp(attrname, "start") == 0) {
 
97
    result=PyInt_FromLong(self->params.start);
 
98
  } else if (strcmp(attrname, "sigma") == 0) {
 
99
    result=PyFloat_FromDouble((double)self->params.sigma);
 
100
  } else if (strcmp(attrname, "info") == 0) {
 
101
    result=PyInt_FromLong(self->params_out.info);
 
102
  } else if (strcmp(attrname, "iter") == 0) {
 
103
    result=PyInt_FromLong(self->params_out.iparam[2]);
 
104
  } else if (strcmp(attrname, "nconv") == 0) {
 
105
    result=PyInt_FromLong(self->params_out.iparam[4]);
 
106
  } else if (strcmp(attrname, "numop") == 0) {
 
107
    result=PyInt_FromLong(self->params_out.iparam[8]);
 
108
  } else if (strcmp(attrname, "numopb") == 0) {
 
109
    result=PyInt_FromLong(self->params_out.iparam[9]);
 
110
  } else if (strcmp(attrname, "numreo") == 0) {
 
111
    result=PyInt_FromLong(self->params_out.iparam[10]);
 
112
  } else {
 
113
    PyErr_SetString(PyExc_AttributeError, attrname);
 
114
  }
 
115
  return result;
 
116
}
 
117
 
 
118
/** \ingroup python_interface_arpack
 
119
 * \brief Sets one of the attributes of a given ARPACK parameters object
 
120
 */
 
121
int igraphmodule_ARPACKOptions_setattr(
 
122
  igraphmodule_ARPACKOptionsObject* self, char* attrname,
 
123
  PyObject* value) {
 
124
  if (value == 0) {
 
125
    PyErr_SetString(PyExc_TypeError, "attribute can not be deleted");
 
126
    return -1;
 
127
  }
 
128
  if (strcmp(attrname, "maxiter") == 0 ||
 
129
      strcmp(attrname, "mxiter") == 0) {
 
130
    if (PyInt_Check(value)) {
 
131
      long int n=PyInt_AsLong(value);
 
132
      if (n>0) self->params.mxiter=n;
 
133
      else {
 
134
        PyErr_SetString(PyExc_ValueError, "maxiter must be positive");
 
135
        return -1;
 
136
      }
 
137
    } else {
 
138
      PyErr_SetString(PyExc_ValueError, "integer expected");
 
139
      return -1;
 
140
    }
 
141
  } else if (strcmp(attrname, "tol") == 0) {
 
142
    if (PyInt_Check(value)) {
 
143
      self->params.tol = (igraph_real_t) PyInt_AsLong(value);
 
144
    } else if (PyFloat_Check(value)) {
 
145
      self->params.tol = (igraph_real_t) PyFloat_AsDouble(value);
 
146
    } else {
 
147
      PyErr_SetString(PyExc_ValueError, "integer or float expected");
 
148
      return -1;
 
149
    }
 
150
  } else {
 
151
    PyErr_SetString(PyExc_AttributeError, attrname);
 
152
    return -1;
 
153
  }
 
154
 
 
155
  return 0;
 
156
}
 
157
 
 
158
/** \ingroup python_interface_arpack */
 
159
igraph_arpack_options_t *igraphmodule_ARPACKOptions_get(
 
160
  igraphmodule_ARPACKOptionsObject *self) {
 
161
  self->params_out = self->params;
 
162
  self->params_out.iparam[0] = self->params.ishift;
 
163
  self->params_out.iparam[2] = self->params.mxiter;
 
164
  self->params_out.iparam[3] = self->params.nb;
 
165
  self->params_out.iparam[6] = self->params.mode;
 
166
  self->params_out.lworkl = 0;
 
167
  self->params_out.info = self->params.start;
 
168
 
 
169
  return &self->params_out;
 
170
}
 
171
 
 
172
/** \ingroup python_interface_arpack
 
173
 * \brief Formats an \c igraph.ARPACKOptions object in a
 
174
 * human-consumable format.
 
175
 * 
 
176
 * \return the formatted textual representation as a \c PyObject
 
177
 */
 
178
PyObject* igraphmodule_ARPACKOptions_str(
 
179
  igraphmodule_ARPACKOptionsObject *self) {
 
180
  PyObject *s;
 
181
  
 
182
  s=PyString_FromFormat("ARPACK parameters");
 
183
  return s;
 
184
}
 
185
 
 
186
/**
 
187
 * \ingroup python_interface_arpack
 
188
 * Method table for the \c igraph.ARPACKOptions object
 
189
 */
 
190
PyMethodDef igraphmodule_ARPACKOptions_methods[] = {
 
191
  /*{"attributes", (PyCFunction)igraphmodule_Edge_attributes,
 
192
      METH_NOARGS,
 
193
      "attributes() -> list\n\n"
 
194
      "Returns the attribute list of the graph's edges\n"
 
195
  },*/
 
196
  {NULL}
 
197
};
 
198
 
 
199
/**
 
200
 * \ingroup python_interface_edge
 
201
 * Getter/setter table for the \c igraph.ARPACKOptions object
 
202
 */
 
203
PyGetSetDef igraphmodule_ARPACKOptions_getseters[] = {
 
204
  /*{"tuple", (getter)igraphmodule_Edge_get_tuple, NULL,
 
205
      "Source and target node index of this edge as a tuple", NULL
 
206
  },*/
 
207
  {NULL}
 
208
};
 
209
 
 
210
/** \ingroup python_interface_edge
 
211
 * Python type object referencing the methods Python calls when it performs
 
212
 * various operations on an ARPACK parameters object
 
213
 */
 
214
PyTypeObject igraphmodule_ARPACKOptionsType = {
 
215
  PyObject_HEAD_INIT(NULL)                    /* */
 
216
  0,                                          /* ob_size */
 
217
  "igraph.ARPACKOptions",                     /* tp_name */
 
218
  sizeof(igraphmodule_ARPACKOptionsObject),   /* tp_basicsize */
 
219
  0,                                          /* tp_itemsize */
 
220
  (destructor)igraphmodule_ARPACKOptions_dealloc,      /* tp_dealloc */
 
221
  0,                                          /* tp_print */
 
222
  (getattrfunc)igraphmodule_ARPACKOptions_getattr,     /* tp_getattr */
 
223
  (setattrfunc)igraphmodule_ARPACKOptions_setattr,     /* tp_setattr */
 
224
  0,                                          /* tp_compare */
 
225
  0,                                          /* tp_repr */
 
226
  0,                                          /* tp_as_number */
 
227
  0,                                          /* tp_as_sequence */
 
228
  0,                                          /* tp_as_mapping */
 
229
  0,                                          /* tp_hash */
 
230
  0,                                          /* tp_call */
 
231
  (reprfunc)igraphmodule_ARPACKOptions_str,   /* tp_str */
 
232
  0,                                          /* tp_getattro */
 
233
  0,                                          /* tp_setattro */
 
234
  0,                                          /* tp_as_buffer */
 
235
  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
 
236
  "Class representing the parameters of the ARPACK module.\n\n"
 
237
  "ARPACK is a Fortran implementation of the implicitly restarted\n"
 
238
  "Arnoldi method, an algorithm for calculating some of the\n"
 
239
  "eigenvalues and eigenvectors of a given matrix. igraph uses this\n"
 
240
  "package occasionally, and this class can be used to fine-tune the\n"
 
241
  "behaviour of ARPACK in such cases.\n\n"
 
242
  "The class has several attributes which are not documented here,\n"
 
243
  "since they are usually of marginal use to the ordinary user.\n"
 
244
  "See the source code of the original ARPACK Fortran package\n"
 
245
  "(especially the file C{dsaupd.f}) for a detailed explanation of the\n"
 
246
  "parameters. Only the most basic attributes are explained here. Most\n"
 
247
  "of them are read only unless stated otherwise.\n\n"
 
248
  " - C{bmat}: type of the eigenproblem solved. C{'I'} means standard\n"
 
249
  "   eigenproblem (A*x = lambda*x), C{'G'} means generalized\n"
 
250
  "   eigenproblem (A*x = lambda*B*x).\n\n"
 
251
  " - C{n}: dimension of the eigenproblem\n\n"
 
252
  " - C{tol}: precision. If less than or equal to zero, the standard\n"
 
253
  "   machine precision is used as computed by the LAPACK utility\n"
 
254
  "   called C{dlamch}. This can be modified.\n\n"
 
255
  " - C{mxiter}: maximum number of update iterations to take. This\n"
 
256
  "   can be modified. You can also use C{maxiter}.\n\n"
 
257
  " - C{iter}: actual number of update iterations taken\n\n"
 
258
  " - C{numop}: total number of OP*x operations\n\n"
 
259
  " - C{numopb}: total number of B*x operations if C{bmat} is C{'G'}\n\n"
 
260
  " - C{numreo}: total number of steps of re-orthogonalization\n\n"
 
261
  "",                              /* tp_doc */
 
262
  0,                                          /* tp_traverse */
 
263
  0,                                          /* tp_clear */
 
264
  0,                                          /* tp_richcompare */
 
265
  0,                                          /* tp_weaklistoffset */
 
266
  0,                                          /* tp_iter */
 
267
  0,                                          /* tp_iternext */
 
268
  igraphmodule_ARPACKOptions_methods,         /* tp_methods */
 
269
  0,                                          /* tp_members */
 
270
  igraphmodule_ARPACKOptions_getseters,       /* tp_getset */
 
271
  0,                                          /* tp_base */
 
272
  0,                                          /* tp_dict */
 
273
  0,                                          /* tp_descr_get */
 
274
  0,                                          /* tp_descr_set */
 
275
  0,                                          /* tp_dictoffset */
 
276
  0,                                          /* tp_init */
 
277
  0,                                          /* tp_alloc */
 
278
  (newfunc)igraphmodule_ARPACKOptions_new,    /* tp_new */
 
279
  0,                                          /* tp_free */
 
280
};
 
281