Browse files

Updated DiagonalMat - not perfect, but it should do for now. Also fix…

…ed another one of those pesky assertions David Simcha found, this time in cowarray.d.
  • Loading branch information...
1 parent feefc2b commit e8d857798c6a8f0c9893a358bdea87ffd60fd67a @cristicbz committed Nov 23, 2011
Showing with 112 additions and 59 deletions.
  1. BIN scid.suo
  2. BIN scid.v11.suo
  3. +10 −3 scid/demo.d
  4. +18 −18 scid/scid.visualdproj
  5. +0 −1 scid/storage/cowarray.d
  6. +83 −35 scid/storage/diagonalmat.d
  7. +1 −2 scid/vector.d
View
BIN scid.suo
Binary file not shown.
View
BIN scid.v11.suo
Binary file not shown.
View
13 scid/demo.d
@@ -2,6 +2,7 @@ module scid.demo;
version( demo ) {
import scid.matvec;
+ import scid.storage.diagonalmat;
import scid.common.traits, scid.common.meta;
import scid.internal.regionallocator;
@@ -10,7 +11,12 @@ version( demo ) {
import std.string, std.math;
void main() {
- testIssue61();
+ auto x = DiagonalMatrix!double([1.,2,3,4]), y=DiagonalMatrix!double([2.,2.,2.,2.]);
+
+ // auto w = SymmetricMatrix!double([[1.,2.,3.,4],[1.,2.,3.,4],[1.,2.,3.,4],[1.,2.,3.,4]]);
+ auto z = DiagonalMatrix!double(0);
+ z[] = x + y;
+ writeln(z.pretty);
readln();
}
@@ -250,7 +256,7 @@ version( demo ) {
/** A TypeTuple of all matrix types to be tested by generic tests. */
template MatrixTypes( T ) {
alias TypeTuple!(
- Matrix!T,
+ //Matrix!T,
//Matrix!(T,StorageOrder.RowMajor),
//TriangularMatrix!T,
//TriangularMatrix!(T, MatrixTriangle.Lower ),
@@ -259,7 +265,8 @@ version( demo ) {
//SymmetricMatrix!T,
// SymmetricMatrix!(T, MatrixTriangle.Lower ),
// SymmetricMatrix!(T, MatrixTriangle.Upper, StorageOrder.RowMajor),
- // SymmetricMatrix!(T, MatrixTriangle.Lower, StorageOrder.RowMajor)
+ // SymmetricMatrix!(T, MatrixTriangle.Lower, StorageOrder.RowMajor),
+ DiagonalMatrix!T
) MatrixTypes;
}
View
36 scid/scid.visualdproj
@@ -9,7 +9,7 @@
<oneobj>0</oneobj>
<trace>0</trace>
<quiet>0</quiet>
- <verbose>1</verbose>
+ <verbose>0</verbose>
<vtls>0</vtls>
<symdebug>0</symdebug>
<optimize>0</optimize>
@@ -451,12 +451,6 @@
<filesToClean>*.obj;*.cmd;*.build;*.json;*.dep</filesToClean>
</Config>
<Folder name="scid">
- <Folder name="ops">
- <File path="ops\expression.d" />
- <File path="ops\fallback.d" />
- <File path="ops\eval.d" />
- <File path="ops\common.d" />
- </Folder>
<Folder name="bindings">
<Folder name="blas">
<File path="bindings\blas\blas.d" />
@@ -472,41 +466,47 @@
<File path="common\fortran.d" />
<File path="common\memory.d" />
<File path="common\meta.d" />
+ <File path="common\storagetraits.d" />
<File path="common\testing.d" />
<File path="common\traits.d" />
- <File path="common\storagetraits.d" />
</Folder>
<Folder name="internal">
- <File path="internal\regionallocator.d" />
<File path="internal\assertmessages.d" />
+ <File path="internal\regionallocator.d" />
+ </Folder>
+ <Folder name="ops">
+ <File path="ops\common.d" />
+ <File path="ops\eval.d" />
+ <File path="ops\expression.d" />
+ <File path="ops\fallback.d" />
</Folder>
<Folder name="storage">
<File path="storage\array.d" />
<File path="storage\arraydata.d" />
<File path="storage\arrayview.d" />
+ <File path="storage\constant.d" />
<File path="storage\cowarray.d" />
<File path="storage\cowmatrix.d" />
+ <File path="storage\diagonalmat.d" />
+ <File path="storage\external.d" />
<File path="storage\generalmat.d" />
<File path="storage\generalmatview.d" />
<File path="storage\packedmat.d" />
<File path="storage\packedsubmat.d" />
<File path="storage\packedsubvec.d" />
<File path="storage\symmetric.d" />
<File path="storage\triangular.d" />
- <File path="storage\external.d" />
- <File path="storage\diagonalmat.d" />
<File path="storage\triangularview.d" />
- <File path="storage\constant.d" />
</Folder>
+ <File path="blas.d" />
+ <File path="demo.d" />
<File path="exception.d" />
- <File path="types.d" />
- <File path="util.d" />
- <File path="vector.d" />
+ <File path="lapack.d" />
<File path="matrix.d" />
<File path="matvec.d" />
- <File path="demo.d" />
<File path="storages.d" />
- <File path="blas.d" />
- <File path="lapack.d" />
+ <File path="types.d" />
+ <File path="util.d" />
+ <File path="vector.d" />
</Folder>
</DProject>
View
1 scid/storage/cowarray.d
@@ -90,7 +90,6 @@ struct CowArray( ElementType_ ) {
in {
checkBounds_( i );
} out {
- assert( index( i ) == rhs );
assert( data_.refCount() == 1 );
} body {
unshareData_();
View
118 scid/storage/diagonalmat.d
@@ -5,6 +5,8 @@ import scid.common.traits;
import scid.common.meta;
import scid.ops.common, scid.ops.expression, scid.ops.eval;
import scid.storage.cowarray;
+import scid.storage.generalmat;
+import scid.storage.external;
import scid.internal.assertmessages;
@@ -35,6 +37,11 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
alias BasicDiagonalMatrixStorage!( ContainerRef_, DiagonalMatrixStorageType.View )
View;
+
+ alias BasicDiagonalMatrixStorage!( ExternalArray!(ElementType, ArrayTypeOf!ContainerRef), DiagonalMatrixStorageType.Root )
+ Temporary;
+
+ alias typeof(this) Transposed;
private enum isRoot = (type_ == DiagonalMatrixStorageType.Root);
private enum isView = (type_ == DiagonalMatrixStorageType.View);
@@ -88,30 +95,36 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
ElementType index( size_t i, size_t j ) const
in {
- assert( i < rows && j < columns, boundsMsg_( i, j ) );
+ checkBounds_( i, j );
} body {
return i == j ? containerRef_.index( i ) : Zero!ElementType;
}
void indexAssign( string op = "" )( ElementType rhs, size_t i, size_t j )
in {
- assert( i < rows && j < columns, boundsMsg_( i, j ) );
- assert( i == j, msgPrefix_ ~ "Assignment to zero element in diagonal matrix." );
+ checkBounds_( i, j );
+ assert( i == j, "Assignment to zero element in diagonal matrix." );
} body {
containerRef_.indexAssign!op( rhs, i );
}
- Slice slice( size_t rowStart, size_t colStart, size_t rowEnd, size_t colEnd ) {
+ Slice slice( size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd )
+ in {
+ checkSliceIndices_( rowStart, rowEnd, colStart, colEnd );
+ } body {
return typeof( return )( containerRef_, rowStart, rowEnd - rowStart, colStart, colEnd - colStart );
}
- View view( size_t rowStart, size_t colStart, size_t rowEnd, size_t colEnd ) {
+ View view( size_t rowStart, size_t rowEnd, size_t colStart, size_t colEnd )
+ in {
+ checkSliceIndices_( rowStart, rowEnd, colStart, colEnd );
+ } body {
return typeof( return )( containerRef_, rowStart, rowEnd - rowStart, colStart, colEnd - colStart );
}
RowView row( size_t i )
in {
- assert( i < rows, sliceMsg_( i, 0, i, columns ) );
+ checkSliceIndices_( i, i, 0, columns );
} body {
if( isRoot || i >= colStart_ )
return typeof( return )( containerRef_, i + rowStart_ , i - colStart_, columns );
@@ -121,22 +134,36 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
ColumnView column( size_t j )
in {
- assert( j < columns, sliceMsg_( 0, j, rows, j ) );
+ checkSliceIndices_( 0, rows, j, j );
} body {
if( isRoot || j >= rowStart_ )
return typeof( return )( containerRef_, j + colStart_, j - rowStart_, rows );
else
return typeof( return )( rows );
}
- void resize( size_t rows, size_t columns, void * ) {
- assert( rows == columns );
- containerRef_.resize( rows, null );
- }
+ static if( is( typeof( containerRef_.resize( 0 ) ) ) ) {
+ void resize( size_t newRows, size_t newColumns, void * ) {
+ static if( !isView ) {
+ checkSquareDims_!"diagonal"( newRows, newColumns );
+ containerRef_.resize( newRows, null );
+ } else {
+ assert( rows == newRows && columns == newColumns, "Diagonal matrix view cannot be resized." );
+ }
+ }
- void resize( size_t rows, size_t columns ) {
- assert( rows == columns );
- containerRef_.resize( rows );
+ void resize( size_t newRows, size_t newColumns ) {
+ static if( !isView ) {
+ checkSquareDims_!"diagonal"( newRows, newColumns );
+ containerRef_.resize( newRows );
+
+ static if( isSlice ) {
+
+ }
+ } else {
+ assert( rows == newRows && columns == newColumns, "Diagonal matrix view cannot be resized." );
+ }
+ }
}
void copy( Transpose tr = Transpose.no, Source )( auto ref Source source ) if( is( Source : typeof(this))) {
@@ -156,8 +183,7 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
}
void copyRight( Transpose tr = Transpose.no, Dest )( auto ref Dest dest ) {
- assert( dest.rows == rows_ && dest.columns == columns_,
- dimMismatch_( dest.rows, dest.columns, "assignment" ) );
+ checkAssignDims_( dest.rows, dest.columns );
auto start = max( rowStart_, colStart_ );
auto end = min( rows_, cols_ );
foreach( i ; start .. end ) {
@@ -185,8 +211,7 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
}
void scaledAdditionRight( Transpose tr = Transpose.no, Source )( ElementType alpha, auto ref Source source ) {
- assert( dest.rows == rows_ && dest.columns == columns_,
- dimMismatch_( dest.rows, dest.columns, "assignment" ) );
+ checkAssignDims_( dest.rows, dest.columns );
auto start = max( rowStart_, colStart_ );
auto end = min( rows_, cols_ );
foreach( i ; start .. end ) {
@@ -199,6 +224,7 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
void matrixProduct( Transpose transA = Transpose.no, Transpose transB = Transpose.no, A,B )
( ElementType alpha, auto ref A a, auto ref B b, ElementType beta ) {
+
auto start = max( rowStart_, colStart_ );
auto end = min( rows_, cols_ );
auto d = containerRef_.data[ start .. end ];
@@ -207,30 +233,52 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
auto n = transB ? b.rows : b.columns;
if( !alpha ) {
- if( !beta ) resize( m, n );
- else {
- assert( rows == m && columns == n );
- blas.scal( d.length, Zero!ElementType, d.ptr, 1 );
+ if( !beta ) {
+ resize( m, n );
+ } else {
+ assert( rows == m && columns == n, "Matrix dimensions mismatch in diagonal matrix product." );
+ blas.scal( d.length, beta, d.ptr, 1 );
}
return;
}
+
//static if( is( A : typeof( this ) ) && is( B : typeof( this ) ) ) {
// blas.sbmv( 'u', d.length, 0, alpha, a.cdata, 1, b.cdata, 1, beta, d.ptr, 1 );
//} else {
if( beta ) {
foreach( i, ref destElem ; d ) {
+ assert( rows == m && columns == n, "Matrix dimensions mismatch in diagonal matrix product." );
destElem *= beta;
destElem += alpha * rowColumnDot!( transA, transB )( a, i, b, i );
}
} else {
+ import std.stdio; writeln("asdf");
+ resize( m, n, null );
+ // get d again, resize will change the ptr
+
+ start = max( rowStart_, colStart_ );
+ end = min( rows_, cols_ );
+ d = containerRef_.data[ start .. end ];
+
foreach( i, ref destElem ; d ) {
destElem = alpha * rowColumnDot!( transA, transB )( a, i, b, i );
}
}
//}
}
+
+ /** Promotions for this type are inherited either from its container or from general matrix. */
+ template Promote( Other ) {
+ static if( isRoot && is( Other : typeof(this) ) ) {
+ alias typeof(this) Promote;
+ } else static if( isScalar!Other ) {
+ alias BasicDiagonalMatrixStorage!( Promotion!(Other,ContainerRef) ) Promote;
+ } else {
+ alias Promotion!( GeneralMatrixStorage!ElementType, Other ) Promote;
+ }
+ }
@property {
const(ElementType)* cdata() const { return containerRef_.cdata; }
@@ -246,7 +294,7 @@ struct BasicDiagonalMatrixStorage( ContainerRef_, DiagonalMatrixStorageType type
}
private:
- mixin MatrixErrorMessages;
+ mixin MatrixChecks;
static if( !isRoot ) {
size_t rowStart_, rows_;
@@ -299,7 +347,7 @@ struct DiagonalMatrixSubVectorStorage( ContainerRef_, VectorType vtype_ ) {
ElementType index( size_t i ) const
in {
- assert( i < length_, boundsMsg_( i ) );
+ checkBounds_( i );
} body {
if( i == fakeIndex_ )
return containerRef_.index( realIndex_ );
@@ -309,8 +357,8 @@ struct DiagonalMatrixSubVectorStorage( ContainerRef_, VectorType vtype_ ) {
void indexAssign( string op = "" )( ElementType rhs, size_t i )
in {
- assert( i < length_, boundsMsg_( i ) );
- assert( i == fakeIndex_, msgPrefix_ ~ "Assignment to zero element in diagonal matrix." );
+ checkBounds_( i );
+ assert( i == fakeIndex_, "Assignment to zero element in diagonal matrix." );
} body {
containerRef_.indexAssign!op( rhs, realIndex_ );
}
@@ -332,7 +380,7 @@ struct DiagonalMatrixSubVectorStorage( ContainerRef_, VectorType vtype_ ) {
void popFront()
in {
- assert( !empty, emptyMsg_("popFront()") );
+ checkNotEmpty_!"popFront()"();
} body {
if( fakeIndex_ > 0 )
-- fakeIndex_;
@@ -343,38 +391,38 @@ struct DiagonalMatrixSubVectorStorage( ContainerRef_, VectorType vtype_ ) {
void popBack()
in {
- assert( !empty, emptyMsg_("popBack()") );
+ checkNotEmpty_!"popBack()"();
} body {
-- length_;
}
void copy( Transpose tr = Transpose.no, Source )( auto ref Source source ) {
- assert( source.length == length_, lengthMismatch_( source.length, "assignment" ) );
+ checkAssignLength_( source.length );
nonZeroAssign_!tr( source.index( fakeIndex_ ), realIndex_ );
}
void scaledAddition( Transpose tr = Transpose.no, Source )( ElementType alpha, auto ref Source source ) {
- assert( source.length == length_, lengthMismatch_( source.length, "scaled addition" ) );
+ checkAssignLength_( source.length );
nonZeroScaledAddition_!tr( alpha, source.index( fakeIndex_ ) );
}
ElementType dot( Transpose tr = Transpose.no, Right )( auto ref Right right ) {
- assert( right.length == length_, lengthMismatch_( right.length, "dot product" ) );
+ assert( right.length == length_, "Length mismatch in dot product." );
if( hasNonZero_() )
return right.index( fakeIndex_ ) * nonZero_!tr();
else
return Zero!ElementType;
}
void copyRight( Transpose tr = Transpose.no, Dest )( auto ref Dest dest ) {
- assert( dest.length == length_, lengthMismatch_( dest.length, "assignment" ) );
+ checkAssignLength_( source.length );
evalScaling( Zero!ElementType, dest );
if( hasNonZero_() )
dest.indexAssign( nonZero_!tr(), fakeIndex_ );
}
void scaledAdditionRight( Transpose tr = Transpose.no, Dest )( ElementType alpha, auto ref Dest dest ) {
- assert( dest.length == length_, lengthMismatch_( dest.length, "scaled addition" ) );
+ checkAssignLength_( source.length );
if( hasNonZero_() )
dest.indexAssign!"+"( alpha * nonZero_!tr(), fakeIndex_ );
}
@@ -390,7 +438,7 @@ struct DiagonalMatrixSubVectorStorage( ContainerRef_, VectorType vtype_ ) {
}
private:
- mixin ArrayErrorMessages;
+ mixin ArrayChecks;
bool hasNonZero_() const {
return fakeIndex_ < length_;
@@ -424,4 +472,4 @@ private:
size_t length_, fakeIndex_, realIndex_;
ContainerRef containerRef_;
-}
+}
View
3 scid/vector.d
@@ -324,8 +324,7 @@ struct BasicVector( Storage_ ) {
static if( isResizable ) {
storage.resize( newLength, null );
} else {
- assert( length == newLength,
- lengthMismatch_( newLength ) );
+ assert( length == newLength );
}
}

0 comments on commit e8d8577

Please sign in to comment.