[pure-lang-svn] SF.net SVN: pure-lang:[802] pure/trunk
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-09-20 08:58:06
|
Revision: 802
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=802&view=rev
Author: agraef
Date: 2008-09-20 08:57:43 +0000 (Sat, 20 Sep 2008)
Log Message:
-----------
Bugfixes.
Modified Paths:
--------------
pure/trunk/lib/primitives.pure
pure/trunk/printer.cc
pure/trunk/runtime.cc
pure/trunk/runtime.h
Modified: pure/trunk/lib/primitives.pure
===================================================================
--- pure/trunk/lib/primitives.pure 2008-09-20 08:20:26 UTC (rev 801)
+++ pure/trunk/lib/primitives.pure 2008-09-20 08:57:43 UTC (rev 802)
@@ -460,7 +460,7 @@
/* Extract a submatrix of a given size at a given offset. */
submat x::matrix (i::int,j::int) (n::int,m::int)
- = matrix_slice x i j (i+n) (j+m);
+ = matrix_slice x i j (i+n-1) (j+m-1);
/* Construct matrices from lists of rows and columns. These take either
scalars or submatrices as inputs; corresponding dimensions must match.
Modified: pure/trunk/printer.cc
===================================================================
--- pure/trunk/printer.cc 2008-09-20 08:20:26 UTC (rev 801)
+++ pure/trunk/printer.cc 2008-09-20 08:57:43 UTC (rev 802)
@@ -764,33 +764,37 @@
}
case EXPR::PTR:
return os << "#<pointer " << x->data.p << ">";
-#ifdef HAVE_GSL
/* NOTE: For performance reasons, we don't do any custom representations for
matrix elements. As a workaround, you can define __show__ on matrices as
a whole. */
case EXPR::MATRIX:
os << "{";
if (x->data.mat.p) {
- prec_t p = sym_nprec(interpreter::g_interp->symtab.pair_sym().f) + 1;
gsl_matrix_symbolic *m = (gsl_matrix_symbolic*)x->data.mat.p;
- for (size_t i = 0; i < m->size1; i++) {
- if (i > 0) os << ";";
- for (size_t j = 0; j < m->size2; j++) {
- if (j > 0) os << ",";
- os << pure_paren(p, m->data[i * m->tda + j]);
+ if (m->size1>0 && m->size2>0) {
+ prec_t p = sym_nprec(interpreter::g_interp->symtab.pair_sym().f) + 1;
+ for (size_t i = 0; i < m->size1; i++) {
+ if (i > 0) os << ";";
+ for (size_t j = 0; j < m->size2; j++) {
+ if (j > 0) os << ",";
+ os << pure_paren(p, m->data[i * m->tda + j]);
+ }
}
}
}
return os << "}";
+#ifdef HAVE_GSL
case EXPR::DMATRIX:
os << "{";
if (x->data.mat.p) {
gsl_matrix *m = (gsl_matrix*)x->data.mat.p;
- for (size_t i = 0; i < m->size1; i++) {
- if (i > 0) os << ";";
- for (size_t j = 0; j < m->size2; j++) {
- if (j > 0) os << ",";
- print_double(os, m->data[i * m->tda + j]);
+ if (m->size1>0 && m->size2>0) {
+ for (size_t i = 0; i < m->size1; i++) {
+ if (i > 0) os << ";";
+ for (size_t j = 0; j < m->size2; j++) {
+ if (j > 0) os << ",";
+ print_double(os, m->data[i * m->tda + j]);
+ }
}
}
}
@@ -799,11 +803,15 @@
os << "{";
if (x->data.mat.p) {
gsl_matrix_int *m = (gsl_matrix_int*)x->data.mat.p;
- for (size_t i = 0; i < m->size1; i++) {
- if (i > 0) os << ";";
- for (size_t j = 0; j < m->size2; j++) {
- if (j > 0) os << ",";
- os << m->data[i * m->tda + j];
+ if (m->size1>0 && m->size2>0) {
+ if (m->size1>0 && m->size2>0) {
+ for (size_t i = 0; i < m->size1; i++) {
+ if (i > 0) os << ";";
+ for (size_t j = 0; j < m->size2; j++) {
+ if (j > 0) os << ",";
+ os << m->data[i * m->tda + j];
+ }
+ }
}
}
}
@@ -812,39 +820,39 @@
os << "{";
if (x->data.mat.p) {
gsl_matrix_complex *m = (gsl_matrix_complex*)x->data.mat.p;
- /* GSL represents complex matrices using pairs of double values, while
- Pure provides its own complex type in math.pure. If math.pure has
- been loaded, then the '+:' operator is defined and we use this
- representation. Otherwise, we print complex values as pairs of real
- and imaginary part. */
- symbol *rect = interpreter::g_interp->symtab.complex_rect_sym();
- if (rect)
- for (size_t i = 0; i < m->size1; i++) {
- if (i > 0) os << ";";
- for (size_t j = 0; j < m->size2; j++) {
- if (j > 0) os << ",";
- print_double(os, m->data[2*(i * m->tda + j)]);
- os << rect->s;
- print_double(os, m->data[2*(i * m->tda + j) + 1]);
+ if (m->size1>0 && m->size2>0) {
+ /* GSL represents complex matrices using pairs of double values, while
+ Pure provides its own complex type in math.pure. If math.pure has
+ been loaded, then the '+:' operator is defined and we use this
+ representation. Otherwise, we print complex values as pairs of real
+ and imaginary part. */
+ symbol *rect = interpreter::g_interp->symtab.complex_rect_sym();
+ if (rect)
+ for (size_t i = 0; i < m->size1; i++) {
+ if (i > 0) os << ";";
+ for (size_t j = 0; j < m->size2; j++) {
+ if (j > 0) os << ",";
+ print_double(os, m->data[2*(i * m->tda + j)]);
+ os << rect->s;
+ print_double(os, m->data[2*(i * m->tda + j) + 1]);
+ }
}
- }
- else
- for (size_t i = 0; i < m->size1; i++) {
- if (i > 0) os << ";";
- for (size_t j = 0; j < m->size2; j++) {
- if (j > 0) os << ",";
- os << "(";
- print_double(os, m->data[2*(i * m->tda + j)]);
- os << ",";
- print_double(os, m->data[2*(i * m->tda + j) + 1]);
- os << ")";
+ else
+ for (size_t i = 0; i < m->size1; i++) {
+ if (i > 0) os << ";";
+ for (size_t j = 0; j < m->size2; j++) {
+ if (j > 0) os << ",";
+ os << "(";
+ print_double(os, m->data[2*(i * m->tda + j)]);
+ os << ",";
+ print_double(os, m->data[2*(i * m->tda + j) + 1]);
+ os << ")";
+ }
}
- }
+ }
}
return os << "}";
#else
- case EXPR::MATRIX:
- return os << "#<matrix " << x->data.mat.p << ">";
case EXPR::DMATRIX:
return os << "#<dmatrix " << x->data.mat.p << ">";
case EXPR::IMATRIX:
Modified: pure/trunk/runtime.cc
===================================================================
--- pure/trunk/runtime.cc 2008-09-20 08:20:26 UTC (rev 801)
+++ pure/trunk/runtime.cc 2008-09-20 08:57:43 UTC (rev 802)
@@ -4053,7 +4053,7 @@
}
extern "C"
-pure_expr *matrix_elem_at(pure_expr *x, uint32_t i)
+pure_expr *matrix_elem_at(pure_expr *x, int32_t i)
{
switch (x->tag) {
case EXPR::MATRIX: {
@@ -4080,7 +4080,7 @@
}
extern "C"
-pure_expr *matrix_elem_at2(pure_expr *x, uint32_t i, uint32_t j)
+pure_expr *matrix_elem_at2(pure_expr *x, int32_t i, int32_t j)
{
switch (x->tag) {
case EXPR::MATRIX: {
@@ -4111,14 +4111,18 @@
}
extern "C"
-pure_expr *matrix_slice(pure_expr *x, uint32_t i1, uint32_t j1,
- uint32_t i2, uint32_t j2)
+pure_expr *matrix_slice(pure_expr *x, int32_t i1, int32_t j1,
+ int32_t i2, int32_t j2)
{
void *p = 0;
+ if (i1<0) i1 = 0; if (j1<0) j1 = 0;
switch (x->tag) {
case EXPR::MATRIX: {
gsl_matrix_symbolic *m = (gsl_matrix_symbolic*)x->data.mat.p;
- size_t n1 = (i2>=i1)?(i2+1-i1):0, n2 = (j2>=j1)?(j2+1-j1):0;
+ if (i2 >= (int)m->size1) i2 = m->size1-1;
+ if (j2 >= (int)m->size2) j2 = m->size2-1;
+ size_t n1 = (i1<(int)m->size1 && i2>=i1)?(i2+1-i1):0,
+ n2 = (j1<(int)m->size2 && j2>=j1)?(j2+1-j1):0;
if (n1 == 0 || n2 == 0) // empty matrix
return pure_symbolic_matrix(create_symbolic_matrix(n1, n2));
gsl_matrix_symbolic_view v =
@@ -4134,7 +4138,10 @@
#ifdef HAVE_GSL
case EXPR::DMATRIX: {
gsl_matrix *m = (gsl_matrix*)x->data.mat.p;
- size_t n1 = (i2>=i1)?(i2+1-i1):0, n2 = (j2>=j1)?(j2+1-j1):0;
+ if (i2 >= (int)m->size1) i2 = m->size1-1;
+ if (j2 >= (int)m->size2) j2 = m->size2-1;
+ size_t n1 = (i1<(int)m->size1 && i2>=i1)?(i2+1-i1):0,
+ n2 = (j1<(int)m->size2 && j2>=j1)?(j2+1-j1):0;
if (n1 == 0 || n2 == 0) // empty matrix
return pure_double_matrix(create_double_matrix(n1, n2));
gsl_matrix_view v = gsl_matrix_submatrix(m, i1, j1, n1, n2);
@@ -4147,7 +4154,10 @@
}
case EXPR::CMATRIX: {
gsl_matrix_complex *m = (gsl_matrix_complex*)x->data.mat.p;
- size_t n1 = (i2>=i1)?(i2+1-i1):0, n2 = (j2>=j1)?(j2+1-j1):0;
+ if (i2 >= (int)m->size1) i2 = m->size1-1;
+ if (j2 >= (int)m->size2) j2 = m->size2-1;
+ size_t n1 = (i1<(int)m->size1 && i2>=i1)?(i2+1-i1):0,
+ n2 = (j1<(int)m->size2 && j2>=j1)?(j2+1-j1):0;
if (n1 == 0 || n2 == 0) // empty matrix
return pure_complex_matrix(create_complex_matrix(n1, n2));
gsl_matrix_complex_view v =
@@ -4162,7 +4172,10 @@
}
case EXPR::IMATRIX: {
gsl_matrix_int *m = (gsl_matrix_int*)x->data.mat.p;
- size_t n1 = (i2>=i1)?(i2+1-i1):0, n2 = (j2>=j1)?(j2+1-j1):0;
+ if (i2 >= (int)m->size1) i2 = m->size1-1;
+ if (j2 >= (int)m->size2) j2 = m->size2-1;
+ size_t n1 = (i1<(int)m->size1 && i2>=i1)?(i2+1-i1):0,
+ n2 = (j1<(int)m->size2 && j2>=j1)?(j2+1-j1):0;
if (n1 == 0 || n2 == 0) // empty matrix
return pure_int_matrix(create_int_matrix(n1, n2));
gsl_matrix_int_view v = gsl_matrix_int_submatrix(m, i1, j1, n1, n2);
Modified: pure/trunk/runtime.h
===================================================================
--- pure/trunk/runtime.h 2008-09-20 08:20:26 UTC (rev 801)
+++ pure/trunk/runtime.h 2008-09-20 08:57:43 UTC (rev 802)
@@ -659,17 +659,17 @@
aren't range-checked, if this is needed you have to do it beforehand,
checking against matrix_size or matrix_dim above. */
-pure_expr *matrix_elem_at(pure_expr *x, uint32_t i);
-pure_expr *matrix_elem_at2(pure_expr *x, uint32_t i, uint32_t j);
+pure_expr *matrix_elem_at(pure_expr *x, int32_t i);
+pure_expr *matrix_elem_at2(pure_expr *x, int32_t i, int32_t j);
/* The following operation retrieves a slice a.k.a. submatrix of a matrix and
returns it as a new matrix object. The result matrix shares the underlying
storage with the input matrix (i.e., matrix elements are *not* copied) and
- so this is a comparatively cheap operation. Indices are zero-based and must
- be checked by the caller if necessary. */
+ so this is a comparatively cheap operation. Indices are zero-based and are
+ clamped to the available index range automatically. */
-pure_expr *matrix_slice(pure_expr *x, uint32_t i1, uint32_t j1,
- uint32_t i2, uint32_t j2);
+pure_expr *matrix_slice(pure_expr *x, int32_t i1, int32_t j1,
+ int32_t i2, int32_t j2);
/* Matrix construction. These work like the pure_matrix_rows/
pure_matrix_columns functions in the public API, but take their input from
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|