1. Fazlul Shahriar
  2. go-gsl

Commits

Fazlul Shahriar  committed 2e2a374

add error handling

  • Participants
  • Parent commits 7a3aa09
  • Branches default

Comments (0)

Files changed (5)

File gsl/Makefile

View file
 
 TARG=gsl
 CGOFILES=\
+	errno.go\
 	matrix.go\
 	vector.go\
 

File gsl/errno.go

View file
+// Copyright (C) 2010  The Go-GSL Authors.
+//
+// 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 3 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, see <http://www.gnu.org/licenses/>.
+
+package gsl
+
+/*
+#include <gsl/gsl_errno.h>
+*/
+import "C"
+
+import "os"
+
+var (
+	EDOM   = C.GSL_EDOM
+	ERANGE = C.GSL_ERANGE
+	ENOMEM = C.GSL_ENOMEM
+	EINVAL = C.GSL_EINVAL
+)
+
+func init() {
+	// Disable the default error handler, which calls abort.
+	C.gsl_set_error_handler_off()
+}
+
+type Error int
+
+func (e Error) String() string {
+	return C.GoString(C.gsl_strerror(C.int(e)))
+}
+
+func NewError(code int) Error {
+	return Error(code)
+}
+
+func errnoToError(code C.int) os.Error {
+	n := int(code)
+	if n == 0 {
+		return nil
+	}
+	return NewError(n)
+}

File gsl/matrix.go

View file
 import "C"
 
 import (
+	"os"
 	"strconv"
 	"unsafe"
 )
 
 // Copying rows and columns
 
-func (m *Matrix) GetRow(i int) *Vector {
-	if 0 > i || i > m.Size1 {
-		return nil
+func (m *Matrix) GetRow(i int) (*Vector, os.Error) {
+	v := NewVector(m.Size2)
+	n := int(C.gsl_matrix_get_row(v.toGSL(), m.toGSL(), C.size_t(i)))
+	if n != 0 {
+		return nil, NewError(n)
 	}
-	v := NewVector(m.Size2)
-	C.gsl_matrix_get_row(v.toGSL(), m.toGSL(), C.size_t(i))
-	return v
+	return v, nil
 }
 
-func (m *Matrix) GetCol(j int) *Vector {
-	if 0 > j || j > m.Size2 {
-		return nil
+func (m *Matrix) GetCol(j int) (*Vector, os.Error) {
+	v := NewVector(m.Size1)
+	n := int(C.gsl_matrix_get_row(v.toGSL(), m.toGSL(), C.size_t(j)))
+	if n != 0 {
+		return nil, NewError(n)
 	}
-	v := NewVector(m.Size1)
-	C.gsl_matrix_get_row(v.toGSL(), m.toGSL(), C.size_t(j))
-	return v
+	return v, nil
 }
 
-func (m *Matrix) SetRow(i int, v *Vector) {
-	C.gsl_matrix_set_row(m.toGSL(), C.size_t(i), v.toGSL())
+func (m *Matrix) SetRow(i int, v *Vector) os.Error {
+	return errnoToError(C.gsl_matrix_set_row(m.toGSL(), C.size_t(i), v.toGSL()))
 }
 
-func (m *Matrix) SetCol(j int, v *Vector) {
-	C.gsl_matrix_set_col(m.toGSL(), C.size_t(j), v.toGSL())
+func (m *Matrix) SetCol(j int, v *Vector) os.Error {
+	return errnoToError(C.gsl_matrix_set_col(m.toGSL(), C.size_t(j), v.toGSL()))
 }
 
 // Matrix operations
 
-func (m *Matrix) Add(n *Matrix) {
-	C.gsl_matrix_add(m.toGSL(), n.toGSL())
+func (m *Matrix) Add(n *Matrix) os.Error {
+	return errnoToError(C.gsl_matrix_add(m.toGSL(), n.toGSL()))
 }
 
-func (m *Matrix) Sub(n *Matrix) {
-	C.gsl_matrix_sub(m.toGSL(), n.toGSL())
+func (m *Matrix) Sub(n *Matrix) os.Error {
+	return errnoToError(C.gsl_matrix_sub(m.toGSL(), n.toGSL()))
 }
 
-func (m *Matrix) MulElements(n *Matrix) {
-	C.gsl_matrix_mul_elements(m.toGSL(), n.toGSL())
+func (m *Matrix) MulElements(n *Matrix) os.Error {
+	return errnoToError(C.gsl_matrix_mul_elements(m.toGSL(), n.toGSL()))
 }
 
-func (m *Matrix) DivElements(n *Matrix) {
-	C.gsl_matrix_div_elements(m.toGSL(), n.toGSL())
+func (m *Matrix) DivElements(n *Matrix) os.Error {
+	return errnoToError(C.gsl_matrix_div_elements(m.toGSL(), n.toGSL()))
 }
 
-func (m *Matrix) Scale(x float64) {
-	C.gsl_matrix_scale(m.toGSL(), C.double(x))
+func (m *Matrix) Scale(x float64) os.Error {
+	return errnoToError(C.gsl_matrix_scale(m.toGSL(), C.double(x)))
 }
 
-func (m *Matrix) AddConstant(x float64) {
-	C.gsl_matrix_add_constant(m.toGSL(), C.double(x))
+func (m *Matrix) AddConstant(x float64) os.Error {
+	return errnoToError(C.gsl_matrix_add_constant(m.toGSL(), C.double(x)))
 }
 
 // Finding maximum and minimum elements of matrices

File gsl/vector.go

View file
 import "C"
 
 import (
+	"os"
 	"strconv"
 	"unsafe"
 )
 	C.gsl_vector_set_zero(v.toGSL())
 }
 
-func (v *Vector) SetBasis(i int) {
-	C.gsl_vector_set_basis(v.toGSL(), C.size_t(i))
+func (v *Vector) SetBasis(i int) os.Error {
+	return errnoToError(C.gsl_vector_set_basis(v.toGSL(), C.size_t(i)))
 }
 
 // Vector views
 
 // Copying vectors
 
-func (v *Vector) Memcpy(src *Vector) {
-	C.gsl_vector_memcpy(v.toGSL(), src.toGSL())
+func (v *Vector) Memcpy(src *Vector) os.Error {
+	return errnoToError(C.gsl_vector_memcpy(v.toGSL(), src.toGSL()))
 }
 
 // Vector operations
 
-func (v *Vector) Add(w *Vector) {
-	C.gsl_vector_add(v.toGSL(), w.toGSL())
+func (v *Vector) Add(w *Vector) os.Error {
+	return errnoToError(C.gsl_vector_add(v.toGSL(), w.toGSL()))
 }
 
-func (v *Vector) Sub(w *Vector) {
-	C.gsl_vector_sub(v.toGSL(), w.toGSL())
+func (v *Vector) Sub(w *Vector) os.Error {
+	return errnoToError(C.gsl_vector_sub(v.toGSL(), w.toGSL()))
 }
 
-func (v *Vector) Mul(w *Vector) {
-	C.gsl_vector_mul(v.toGSL(), w.toGSL())
+func (v *Vector) Mul(w *Vector) os.Error {
+	return errnoToError(C.gsl_vector_mul(v.toGSL(), w.toGSL()))
 }
 
-func (v *Vector) Div(w *Vector) {
-	C.gsl_vector_div(v.toGSL(), w.toGSL())
+func (v *Vector) Div(w *Vector) os.Error {
+	return errnoToError(C.gsl_vector_div(v.toGSL(), w.toGSL()))
 }
 
-func (v *Vector) AddConstant(x float64) {
-	C.gsl_vector_add_constant(v.toGSL(), C.double(x))
+func (v *Vector) Scale(x float64) os.Error {
+	return errnoToError(C.gsl_vector_scale(v.toGSL(), C.double(x)))
 }
 
-func (v *Vector) Scale(x float64) {
-	C.gsl_vector_scale(v.toGSL(), C.double(x))
+func (v *Vector) AddConstant(x float64) os.Error {
+	return errnoToError(C.gsl_vector_add_constant(v.toGSL(), C.double(x)))
 }
 
 // Finding maximum and minimum elements of vectors

File gsl/vector_test.go

View file
 	}
 }
 
+func TestMemcpy(t *testing.T) {
+	v := NewVector(10)
+	w := NewVector(15)
+	err := v.Memcpy(w)
+	if err == nil {
+		t.Errorf("Got nil error; expected a non-nil error\n")
+	}
+}
+
 func BenchmarkString(b *testing.B) {
 	b.StopTimer()
 	v := NewVector(1000)