From 98baa52745d355357d2cb7c453bc90256a7b5c31 Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Fri, 18 Nov 2016 13:38:19 +0000 Subject: [PATCH] Enable/fix boolean vectors and matrices in ruby, java, octave and csharp --- examples/meta/generator/targets/csharp.json | 8 +++---- examples/meta/generator/targets/java.json | 12 +++++----- examples/meta/generator/targets/octave.json | 10 ++++---- examples/meta/generator/targets/ruby.json | 14 ++++++----- examples/meta/src/meta_api/matrix_types.sg | 15 ++++++------ examples/meta/src/meta_api/vector_types.sg | 7 +++--- src/interfaces/java_modular/swig_typemaps.i | 18 ++++++++++---- src/shogun/lib/SGMatrix.h | 26 +++++++++++++++++++++ src/shogun/lib/SGVector.cpp | 14 ----------- src/shogun/lib/SGVector.h | 19 +++++++++------ 10 files changed, 87 insertions(+), 56 deletions(-) diff --git a/examples/meta/generator/targets/csharp.json b/examples/meta/generator/targets/csharp.json index b4450a612fc..50d40a6baf0 100644 --- a/examples/meta/generator/targets/csharp.json +++ b/examples/meta/generator/targets/csharp.json @@ -15,7 +15,6 @@ "RealVector": "var $name = new double[$arguments]", "LongRealVector": "var $name = new double[$arguments]", "ComplexVector": "var $name = new Complex[$arguments]", - "BoolMatrix": "var $name = new bool[$arguments]", "CharMatrix": "var $name = new char[$arguments]", "ByteMatrix": "var $name = new byte[$arguments]", "WordMatrix": "var $name = new ushort[$arguments]", @@ -47,7 +46,6 @@ "RealVector": "double[]", "LongRealVector": "double[]", "ComplexVector": "Complex[]", - "BoolMatrix": "bool[,]", "CharMatrix": "char[,]", "ByteMatrix": "byte[,]", "WordMatrix": "ushort[,]", @@ -79,12 +77,14 @@ "Access": { "Vector": "$identifier[$indices]", "Matrix": "$identifier[$indices]", - "BoolVector": "$identifier.get_element($indices)" + "BoolVector": "$identifier.get_element($indices)", + "BoolMatrix": "$identifier.get_element($indices)" }, "Assign": { "Vector": "$identifier[$indices] = $expr", "Matrix": "$identifier[$indices] = $expr", - "BoolVector": "$identifier.set_element($expr, $indices)" + "BoolVector": "$identifier.set_element($expr, $indices)", + "BoolMatrix": "$identifier.set_element($expr, $indices)" }, "ZeroIndexed": true }, diff --git a/examples/meta/generator/targets/java.json b/examples/meta/generator/targets/java.json index ad3707176d8..96dc57db2b4 100644 --- a/examples/meta/generator/targets/java.json +++ b/examples/meta/generator/targets/java.json @@ -11,7 +11,6 @@ "Init": { "Construct": "$typeName $name = new $typeName($arguments)", "Copy": "$typeName $name = $expr", - "BoolVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", "CharVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", "ByteVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", "WordVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", @@ -21,7 +20,6 @@ "ShortRealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", "RealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", "LongRealVector": "DoubleMatrix $name = new DoubleMatrix(1, $arguments)", - "BoolMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)", "CharMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)", "ByteMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)", "WordMatrix": "DoubleMatrix $name = new DoubleMatrix($arguments)", @@ -42,7 +40,6 @@ "real": "double", "string": "String", "char": "char", - "BoolVector": "DoubleMatrix", "CharVector": "DoubleMatrix", "ByteVector": "DoubleMatrix", "WordVector": "DoubleMatrix", @@ -54,7 +51,6 @@ "RealVector": "DoubleMatrix", "LongRealVector": "DoubleMatrix", "ComplexVector": "DoubleMatrix", - "BoolMatrix": "DoubleMatrix", "CharMatrix": "DoubleMatrix", "ByteMatrix": "DoubleMatrix", "WordMatrix": "DoubleMatrix", @@ -85,11 +81,15 @@ "Element": { "Access": { "Vector": "$identifier.get($indices)", - "Matrix": "$identifier.get($indices)" + "Matrix": "$identifier.get($indices)", + "BoolVector": "$identifier.get_element($indices)", + "BoolMatrix": "$identifier.get_element($indices)" }, "Assign": { "Vector": "$identifier.put($indices, $expr)", - "Matrix": "$identifier.put($indices, $expr)" + "Matrix": "$identifier.put($indices, $expr)", + "BoolVector": "$identifier.set_element($expr, $indices)", + "BoolMatrix": "$identifier.set_element($expr, $indices)" }, "ZeroIndexed": true }, diff --git a/examples/meta/generator/targets/octave.json b/examples/meta/generator/targets/octave.json index b63e4148198..010aa47f040 100644 --- a/examples/meta/generator/targets/octave.json +++ b/examples/meta/generator/targets/octave.json @@ -5,7 +5,6 @@ "Init": { "Construct": "$name = $typeName($arguments)", "Copy": "$name = $expr", - "BoolVector": "$name = zeros(1, $arguments, 'int8')", "CharVector": "$name = zeros(1, $arguments, 'char')", "ByteVector": "$name = zeros(1, $arguments, 'int8')", "WordVector": "$name = zeros(1, $arguments, 'int16')", @@ -15,7 +14,6 @@ "ShortRealVector": "$name = zeros(1, $arguments, 'float32')", "RealVector": "$name = zeros(1, $arguments, 'float64')", "LongRealVector": "$name = zeros(1, $arguments, 'float64')", - "BoolMatrix": "$name = zeros($arguments, 'int8')", "CharMatrix": "$name = zeros($arguments, 'char')", "ByteMatrix": "$name = zeros($arguments, 'int8')", "WordMatrix": "$name = zeros($arguments, 'int16')", @@ -48,11 +46,15 @@ "Element": { "Access": { "Vector": "$identifier($indices)", - "Matrix": "$identifier($indices)" + "Matrix": "$identifier($indices)", + "BoolVector": "$identifier.get_element($indices)", + "BoolMatrix": "$identifier.get_element($indices)" }, "Assign": { "Vector": "$identifier($indices) = $expr", - "Matrix": "$identifier($indices) = $expr" + "Matrix": "$identifier($indices) = $expr", + "BoolVector": "$identifier.set_element($expr, $indices)", + "BoolMatrix": "$identifier.set_element($expr, $indices)" }, "ZeroIndexed": false }, diff --git a/examples/meta/generator/targets/ruby.json b/examples/meta/generator/targets/ruby.json index bc6e864760d..d6dd8c9441a 100644 --- a/examples/meta/generator/targets/ruby.json +++ b/examples/meta/generator/targets/ruby.json @@ -5,7 +5,6 @@ "Init": { "Construct": "$name = Modshogun::$typeName.new $arguments", "Copy": "$name = $expr", - "BoolVector": "$name = NArray.byte($arguments)", "CharVector": "$name = NArray.byte($arguments)", "ByteVector": "$name = NArray.byte($arguments)", "WordVector": "$name = NArray.byte($arguments)", @@ -16,7 +15,6 @@ "RealVector": "$name = NArray.float($arguments)", "LongRealVector": "$name = NArray.float($arguments)", "ComplexVector": "$name = NArray.scomplex($arguments)", - "BoolMatrix": "$name = NMatrix.byte($arguments)", "CharMatrix": "$name = NMatrix.float($arguments)", "ByteMatrix": "$name = NMatrix.byte($arguments)", "WordMatrix": "$name = NMatrix.byte($arguments)", @@ -36,8 +34,8 @@ "StringLiteral": "\"$literal\"", "CharLiteral": "'$literal'", "BoolLiteral": { - "True": "1", - "False": "0" + "True": "true", + "False": "false" }, "IntLiteral": "$number", "RealLiteral": "$number", @@ -50,11 +48,15 @@ "Element": { "Access": { "Vector": "$identifier[$indices]", - "Matrix": "$identifier[$indices]" + "Matrix": "$identifier[$indices]", + "BoolVector": "$identifier.get_element $indices", + "BoolMatrix": "$identifier.get_element $indices" }, "Assign": { "Vector": "$identifier[$indices] = $expr", - "Matrix": "$identifier[$indices] = $expr" + "Matrix": "$identifier[$indices] = $expr", + "BoolVector": "$identifier.set_element $expr, $indices", + "BoolMatrix": "$identifier.set_element $expr, $indices" }, "ZeroIndexed": true }, diff --git a/examples/meta/src/meta_api/matrix_types.sg b/examples/meta/src/meta_api/matrix_types.sg index 62d6d3bd86f..584350ea0a1 100644 --- a/examples/meta/src/meta_api/matrix_types.sg +++ b/examples/meta/src/meta_api/matrix_types.sg @@ -1,11 +1,12 @@ BoolMatrix bool_matrix(2,3) -#bool_matrix[0,0]=False -#bool_matrix[1,0]=True -#bool_matrix[0,1]=False -#bool_matrix[1,1]=True -#bool_matrix[0,2]=False -#bool_matrix[1,2]=True -bool_matrix[0,0]=bool_matrix[1,0] +bool_matrix[0,0]=False +bool_matrix[1,0]=True +bool_matrix[0,1]=False +bool_matrix[1,1]=True +bool_matrix[0,2]=False +bool_matrix[1,2]=True +bool bool_element=bool_matrix[1,0] +bool_matrix[0,0]=bool_element ByteMatrix byte_matrix(2,3) byte_matrix[0,0]=0 diff --git a/examples/meta/src/meta_api/vector_types.sg b/examples/meta/src/meta_api/vector_types.sg index 81d87fcb0be..de8baebe32a 100644 --- a/examples/meta/src/meta_api/vector_types.sg +++ b/examples/meta/src/meta_api/vector_types.sg @@ -1,7 +1,8 @@ BoolVector bool_vector(2) -#bool_vector[0]=False -#bool_vector[1]=True -bool_vector[0]=bool_vector[1] +bool_vector[0]=False +bool_vector[1]=True +bool bool_element=bool_vector[1] +bool_vector[0]=bool_element ByteVector byte_vector(2) byte_vector[0]=0 diff --git a/src/interfaces/java_modular/swig_typemaps.i b/src/interfaces/java_modular/swig_typemaps.i index 117e5f52b9a..48543306456 100644 --- a/src/interfaces/java_modular/swig_typemaps.i +++ b/src/interfaces/java_modular/swig_typemaps.i @@ -7,6 +7,14 @@ * Written (W) 2011 Baozeng Ding * */ + +/* + * NOTE: Disabled boolean vector and matrix typemaps due to jblas is not able + * to represent them. + * Once we use a proper matrix library, can re-enable them again. + * See github issue 3554 + * Heiko Strathmann + */ %include @@ -148,7 +156,7 @@ import org.ujmp.core.booleanmatrix.impl.DefaultDenseBooleanMatrix2D; %enddef /*Define concrete examples of the TYPEMAP_SGVector macros */ -TYPEMAP_SGVECTOR(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") +//TYPEMAP_SGVECTOR(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGVECTOR(char, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGVECTOR(uint8_t, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGVECTOR(int16_t, short, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") @@ -282,7 +290,7 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMa %enddef /*Define concrete examples of the TYPEMAP_SGVECTOR macros */ -TYPEMAP_SGVECTOR(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") +//TYPEMAP_SGVECTOR(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") TYPEMAP_SGVECTOR(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGVECTOR(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGVECTOR(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V") @@ -377,7 +385,7 @@ TYPEMAP_SGVECTOR(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D", " %enddef /*Define concrete examples of the TYPEMAP_SGVECTOR_REF macros */ -TYPEMAP_SGVECTOR_REF(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") +//TYPEMAP_SGVECTOR_REF(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") TYPEMAP_SGVECTOR_REF(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGVECTOR_REF(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGVECTOR_REF(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V") @@ -491,7 +499,7 @@ TYPEMAP_SGVECTOR_REF(float64_t, double, Double, jdouble, "toDoubleArray", "()[[D %enddef /*Define concrete examples of the TYPEMAP_SGMATRIX macros */ -TYPEMAP_SGMATRIX(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") +//TYPEMAP_SGMATRIX(bool, boolean, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGMATRIX(char, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGMATRIX(uint8_t, byte, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") TYPEMAP_SGMATRIX(int16_t, short, Double, jdouble, "()[D", "org/jblas/DoubleMatrix", "(II[D)V") @@ -608,7 +616,7 @@ TYPEMAP_SGMATRIX(float64_t, double, Double, jdouble, "()[D", "org/jblas/DoubleMa %enddef /*Define concrete examples of the TYPEMAP_SGMATRIX macros */ -TYPEMAP_SGMATRIX(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") +//TYPEMAP_SGMATRIX(bool, boolean, Boolean, jboolean, "toBooleanArray", "()[[Z", "org/ujmp/core/booleanmatrix/impl/DefaultDenseBooleanMatrix2D", "([BII)V") TYPEMAP_SGMATRIX(char, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGMATRIX(uint8_t, byte, Byte, jbyte, "toByteArray", "()[[B", "org/ujmp/core/bytematrix/impl/DefaultDenseByteMatrix2D", "([BII)V") TYPEMAP_SGMATRIX(int16_t, short, Short, jshort, "toShortArray", "()[[S", "org/ujmp/core/shortmatrix/impl/DefaultDenseShortMatrix2D", "([SII)V") diff --git a/src/shogun/lib/SGMatrix.h b/src/shogun/lib/SGMatrix.h index 9cce27d49fb..0ad257556db 100644 --- a/src/shogun/lib/SGMatrix.h +++ b/src/shogun/lib/SGMatrix.h @@ -165,6 +165,32 @@ template class SGMatrix : public SGReferencedData return matrix[index]; } +#endif // SWIG should skip this part + + /** Get element at index + * + * @param row row index + * @param col column index + * @return element at index + */ + const T& get_element(index_t row, index_t col) + { + return (*this)(row, col); + } + + /** Set element at index + * + * @param el element to set + * @param row row index + * @param col column index + */ + void set_element(const T& el, index_t row, index_t col) + { + (*this)(row, col)=el; + } + +#ifndef SWIG // SWIG should skip this part + /** * Get the matrix (no copying is done here) * diff --git a/src/shogun/lib/SGVector.cpp b/src/shogun/lib/SGVector.cpp index a0f2fada3e5..5ec0e07ff6d 100644 --- a/src/shogun/lib/SGVector.cpp +++ b/src/shogun/lib/SGVector.cpp @@ -239,20 +239,6 @@ void SGVector::range_fill_vector(complex128_t* vec, Not supported for complex128_t\n"); } -template -const T& SGVector::get_element(index_t index) -{ - REQUIRE(vector && (index>=0) && (index -void SGVector::set_element(const T& p_element, index_t index) -{ - REQUIRE(vector && (index>=0) && (index void SGVector::resize_vector(int32_t n) { diff --git a/src/shogun/lib/SGVector.h b/src/shogun/lib/SGVector.h index a18391ede69..411d5d94503 100644 --- a/src/shogun/lib/SGVector.h +++ b/src/shogun/lib/SGVector.h @@ -161,20 +161,25 @@ template class SGVector : public SGReferencedData static void random_vector(T* vec, int32_t len, T min_value, T max_value); #endif // SWIG // SWIG should skip this part - /** Get vector element at index + /** Get element at index * * @param index index - * @return vector element at index + * @return element at index */ - const T& get_element(index_t index); + const T& get_element(index_t index) + { + return (*this)[index]; + } - /** Set vector element at index 'index' return false in case of trouble + /** Set element at index * - * @param p_element vector element to set + * @param el element to set * @param index index - * @return if setting was successful */ - void set_element(const T& p_element, index_t index); + void set_element(const T& el, index_t index) + { + (*this)[index]=el; + } #ifndef SWIG // SWIG should skip this part /** Resize vector