On Wed, Jul 28, 2010 at 11:38 PM, Sergey Bochkanov
<[email protected]> wrote:
> My proposal is to make
> * boolean vector/matrix = GF(2), RDF (non-zero = True)
> * integer vector/matrix = RDF
> * real = RDF
> * complex = CDF
I've attached a patch to make alglib allow input in the following formats:
* boolean vector/matrix: GF(2), RDF, numpy.bool, numpy.float64
* integer vector/matrix: RDF, numpy.int32, numpy.float64
* real: RDF, numpy.float64
* complex: CDF, numpy.complex128
This is entirely untested, except that the following examples (adapted
from your "ALGLIB for Sage" page, and modified to use Sage or numpy
types) work:
sage: from alglib import conv
sage: conv.convr1d( # real convolution of signal and response
....: vector(RDF, [1, 0, 0, 0.1]), # signal
....: 4, # signal length
....: vector(RDF, [1, 0.1, 0.01]), # response
....: 3) # response length
[1.0, 0.10000000000000001, 0.01, 0.10000000000000001,
0.010000000000000002, 0.001]
sage: from alglib import conv
sage: conv.convr1d( # real convolution of signal and response
....: vector(RDF, [1, 0, 0, 0.1]).numpy(), # signal
....: 4, # signal length
....: vector(RDF, [1, 0.1, 0.01]).numpy(), # response
....: 3) # response length
[1.0, 0.10000000000000001, 0.01, 0.10000000000000001,
0.010000000000000002, 0.001]
sage: from alglib import rcond, densesolver
sage: a = matrix(RDF, [[ 1.0, 0.1],
....: [-0.1, 1.0]])
sage: b = vector(RDF, [ 1.1, 0.9])
sage: rcond.rmatrixrcond1( # condition number
....: a, # matrix
....: 2) # matrix size
0.83471074380165278
sage: info, rep, x = densesolver.rmatrixsolve(a, 2, b) # solve linear system
sage: x # solution
[1.0, 1.0]
sage: rep.r1 # condition number
0.83471074380165278
sage: from alglib import rcond, densesolver
sage: a = matrix(RDF, [[ 1.0, 0.1],
....: [-0.1, 1.0]]).numpy()
sage: b = vector(RDF, [ 1.1, 0.9]).numpy()
sage: rcond.rmatrixrcond1( # condition number
....: a, # matrix
....: 2) # matrix size
0.83471074380165278
sage: info, rep, x = densesolver.rmatrixsolve(a, 2, b) # solve linear system
sage: x # solution
[1.0, 1.0]
sage: rep.r1 # condition number
0.83471074380165278
(So I would not be at all surprised if there are bugs in my code.)
Note that I did not touch *_vector_from_x, *_matrix_from_x; so results
are still returned as lists. Also, I didn't worry about efficiency.
Carl
--
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
--- ORIG._alglib.py 2010-08-02 18:52:05.000000000 -0700
+++ _alglib.py 2010-08-02 19:34:40.000000000 -0700
@@ -15,6 +15,12 @@
import ctypes
import sys
import os
+import numpy
+from sage.matrix.all import is_Matrix
+from sage.modules.all import is_FreeModuleElement
+from sage.rings.all import RDF, CDF, GF
+
+GF_2 = GF(2)
DT_BOOL = 1
DT_INT = 2
@@ -163,9 +169,9 @@
# 'msg' parameter to generate error message
#
def safe_len(msg,v):
- if type(v)!=list:
- raise ValueError(msg)
- return len(v)
+ if isinstance(v, (list, numpy.ndarray)) or is_FreeModuleElement(v):
+ return len(v)
+ raise ValueError(msg)
#
# safe matrix size
@@ -176,6 +182,10 @@
#
def safe_cols(msg,v):
if type(v)!=list:
+ if is_Matrix(v):
+ return v.ncols()
+ if isinstance(v, numpy.ndarray) and v.ndim == 2:
+ return v.shape[1]
raise ValueError(msg)
if len(v)==0:
return 0
@@ -198,6 +208,10 @@
#
def safe_rows(msg,v):
if type(v)!=list:
+ if is_Matrix(v):
+ return v.nrows()
+ if isinstance(v, numpy.ndarray) and v.ndim == 2:
+ return v.shape[0]
raise ValueError(msg)
if len(v)==0:
return 0
@@ -213,6 +227,10 @@
def is_bool_vector(v):
if type(v)!=list:
+ if is_FreeModuleElement(v) and (v.base_ring() == GF_2 or v.base_ring() == RDF):
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 1 and (v.dtype == numpy.bool or v.dtype == numpy.float64):
+ return True
return False
for x in v:
try:
@@ -223,6 +241,10 @@
def is_bool_matrix(v):
if type(v)!=list:
+ if is_Matrix(v) and (v.base_ring() == GF_2 or v.base_ring() == RDF):
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 2 and (v.dtype == numpy.bool or v.dtype == numpy.float64):
+ return True
return False
if len(v)==0:
return True
@@ -244,6 +266,10 @@
def is_int_vector(v):
if type(v)!=list:
+ if is_FreeModuleElement(v) and v.base_ring() == RDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 1 and v.dtype == numpy.int32:
+ return True
return False
for x in v:
try:
@@ -254,6 +280,10 @@
def is_int_matrix(v):
if type(v)!=list:
+ if is_Matrix(v) and v.base_ring() == RDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 2 and v.dtype == numpy.int32:
+ return True
return False
if len(v)==0:
return True
@@ -275,6 +305,10 @@
def is_real_vector(v):
if type(v)!=list:
+ if is_FreeModuleElement(v) and v.base_ring() == RDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 1 and v.dtype == numpy.float64:
+ return True
return False
for x in v:
try:
@@ -285,6 +319,10 @@
def is_real_matrix(v):
if type(v)!=list:
+ if is_Matrix(v) and v.base_ring() == RDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 2 and v.dtype == numpy.float64:
+ return True
return False
if len(v)==0:
return True
@@ -306,6 +344,10 @@
def is_complex_vector(v):
if type(v)!=list:
+ if is_FreeModuleElement(v) and v.base_ring() == CDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 1 and v.dtype == numpy.complex128:
+ return True
return False
for x in v:
try:
@@ -316,6 +358,10 @@
def is_complex_matrix(v):
if type(v)!=list:
+ if is_Matrix(v) and v.base_ring() == CDF:
+ return True
+ if isinstance(v, numpy.ndarray) and v.ndim == 2 and v.dtype == numpy.complex128:
+ return True
return False
if len(v)==0:
return True
@@ -449,7 +495,10 @@
#
# determine size
#
- rows = len(v)
+ if is_Matrix(v):
+ rows = v.nrows()
+ else:
+ rows = len(v)
if rows>0:
cols = len(v[0])
else: