--- 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:
