Skip to content

Commit

Permalink
ComplexMatrix2D.get_integer* returns the magnitude of the complex val…
Browse files Browse the repository at this point in the history
…ue, not just the real part of it. This matches behavior of Parrot's Complex PMC, and makes a few more tests pass. Also, fix the row_combine test. NQP is casting the complex values to floats for the addition, which was screwing up the result. Be explicit that the arithmetic should happen by the Complex PMC's rules
  • Loading branch information
Whiteknight committed Aug 19, 2010
1 parent 95996f9 commit df0721b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
29 changes: 21 additions & 8 deletions src/pmc/complexmatrix2d.pmc
Expand Up @@ -435,26 +435,31 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix provides numericmatrix
*/

VTABLE FLOATVAL get_number_keyed(PMC * key) {
/* TODO: What should we return here, the real value or the absolute value? */
DECLATTRS(SELF, attrs);
const INTVAL rows_size = attrs->rows;
const INTVAL cols_size = attrs->cols;
INTVAL rows, cols;
FLOATVAL real, imag;
GET_KEY_INDICES_ROWMAJOR(INTERP, key, rows, cols);
if (rows < 0 || rows >= rows_size || cols >= cols_size || cols < 0)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": indices out of bounds");
return R_ITEM_XY(attrs->storage, attrs->flags, rows_size, cols_size, rows, cols);
real = R_ITEM_XY(attrs->storage, attrs->flags, rows_size, cols_size, rows, cols);
imag = I_ITEM_XY(attrs->storage, attrs->flags, rows_size, cols_size, rows, cols);
return sqrt(real * real + imag + imag);
}

VTABLE FLOATVAL get_number_keyed_int(INTVAL key) {
DECLATTRS(SELF, attrs);
const INTVAL totalsize = attrs->rows * attrs->cols;
FLOATVAL real, imag;
if (key >= totalsize || key < 0) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": Index out of bounds.");
}
return attrs->storage[key * 2];
real = attrs->storage[key * 2];
imag = attrs->storage[key * 2 + 1];
return sqrt(real * real + imag * imag);
}

VTABLE INTVAL get_integer_keyed(PMC * key) {
Expand Down Expand Up @@ -552,6 +557,14 @@ pmclass ComplexMatrix2D dynpmc auto_attrs provides matrix provides numericmatrix
VTABLE_set_pmc_keyed(INTERP, SELF, key, item);
}

VTABLE void set_integer_keyed(PMC * key, INTVAL value) {
VTABLE_set_number_keyed(INTERP, SELF, key, (FLOATVAL)value);
}

VTABLE void set_integer_keyed_int(INTVAL key, INTVAL value) {
VTABLE_set_number_keyed_int(INTERP, SELF, key, (FLOATVAL)value);
}

VTABLE void set_string_keyed_int(INTVAL key, STRING * value) {
DECLATTRS(SELF, attrs);
INTVAL row, col;
Expand Down Expand Up @@ -1330,11 +1343,11 @@ Calculates the matrix equation:

METHOD gemm(PMC *alpha, PMC * A, PMC *B, PMC *beta, PMC *C) {
PMC * const c_out = VTABLE_clone(INTERP, C);
//if (A->vtable->base_type != B->vtable->base_type &&
// C->vtable->base_type != A->vtable->base_type &&
// A->vtable->base_type != enum_class_NumMatrix2D)
// Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
// PLATYPENAME ": gemm only accepts NumMatrix2D arguments");
if (A->vtable->base_type != B->vtable->base_type &&
C->vtable->base_type != A->vtable->base_type &&
A->vtable->base_type != SELF->vtable->base_type)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
PLATYPENAME ": gemm only accepts NumMatrix2D arguments");
call_gemm_pmc(INTERP, alpha, A, B, beta, c_out);
RETURN(PMC* c_out);
}
Expand Down
18 changes: 17 additions & 1 deletion t/testlib/numericmatrixtest.nqp
Expand Up @@ -387,7 +387,23 @@ class Pla::Matrix::NumericMatrixTest is Pla::Matrix::MatrixTest {

method test_METHOD_row_combine() {
my $A := self.fancymatrix2x2();
my $B := self.matrix2x2(self.fancyvalue(0) + self.fancyvalue(2), self.fancyvalue(1) + self.fancyvalue(3),
my $val1;
my $val2;
Q:PIR {
.local pmc me
me = find_lex "self"
$P0 = me."fancyvalue"(0)
$P1 = me."fancyvalue"(1)
$P2 = me."fancyvalue"(2)
$P3 = me."fancyvalue"(3)
$P4 = $P0 + $P2
$P5 = $P1 + $P3
store_lex "$val1", $P4
store_lex "$val2", $P5
};
my $B := self.matrix2x2($val1, $val2,
self.fancyvalue(2), self.fancyvalue(3));
$A.row_combine(1, 0, 1);
assert_equal($A, $B, "cannot row_combine");
Expand Down

0 comments on commit df0721b

Please sign in to comment.