1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2014 by SiegeLord
//
// All rights reserved. Distributed under LGPL 3.0. For full terms see the file LICENSE.

use transpose::Transposer;
use row_accessor::RowAccessor;
use column_accessor::ColumnAccessor;
use elements::MatrixElements;
use hstack::HStack;
use vstack::VStack;
use matrix_mul::MatrixMul;
use view::View;
use slice::Slice;
use reshape::Reshape;
use matrix::Matrix;
use index::{MatrixIndexGet, MatrixIndexSet};

pub trait MatrixRawGet
{
	unsafe fn raw_get(&self, r: usize, c: usize) -> f64;
}

pub trait MatrixRawSet
{
	unsafe fn raw_set(&self, r: usize, c: usize, val: f64);
}

pub trait MatrixGet<T>
{
	unsafe fn unsafe_get(&self, idx: T) -> f64;
	fn get(&self, idx: T) -> f64;
}

pub trait MatrixSet<T>
{
	unsafe fn unsafe_set(&self, idx: T, val: f64);
	fn set(&self, idx: T, val: f64);
}

pub trait MatrixShape
{
	fn ncol(&self) -> usize;
	fn nrow(&self) -> usize;
	#[inline]
	fn size(&self) -> (usize, usize)
	{
		(self.nrow(), self.ncol())
	}
	#[inline]
	fn len(&self) -> usize
	{
		self.nrow() * self.ncol()
	}
}

/* Hack necessary for operator overloading */
pub trait SameShape
{
	fn same_shape(&self, nrow: usize, ncol: usize) -> bool;
}

pub trait MatrixTranspose where Self: Sized
{
	fn t(self) -> Transposer<Self>;
}

pub trait MatrixRowAccess where Self: Sized
{
	unsafe fn unsafe_row(self, row: usize) -> RowAccessor<Self>;
	fn row(self, row: usize) -> RowAccessor<Self>;
}

pub trait MatrixColumnAccess where Self: Sized
{
	unsafe fn unsafe_col(self, col: usize) -> ColumnAccessor<Self>;
	fn col(self, col: usize) -> ColumnAccessor<Self>;
}

pub trait MatrixView<RowRangeType, ColRangeType> where Self: Sized
{
	unsafe fn unsafe_view(self, row_range: RowRangeType, col_range: ColRangeType) -> View<Self>;
	fn view(self, row_range: RowRangeType, col_range: ColRangeType) -> View<Self>;
}

pub trait MatrixReshape where Self: Sized
{
	unsafe fn unsafe_reshape(self, nrow: usize, ncol: usize) -> Reshape<Self>;
	fn reshape(self, nrow: usize, ncol: usize) -> Reshape<Self>;
}

pub trait MatrixSlice<RangeType> where Self: Sized
{
	unsafe fn unsafe_slice(self, range: RangeType) -> Slice<Self>;
	fn slice(self, range: RangeType) -> Slice<Self>;
}

pub trait MatrixAssign<RHS>
{
	unsafe fn unsafe_assign(&self, m: RHS);
	fn assign(&self, m: RHS);
}

pub trait MatrixMultiply<RHS> where Self: Sized
{
	unsafe fn unsafe_mat_mul(self, rhs: RHS) -> Matrix;
	unsafe fn unsafe_mat_mul_lazy(self, rhs: RHS) -> MatrixMul<Self, RHS>;
	fn mat_mul(self, rhs: RHS) -> Matrix;
	fn mat_mul_lazy(self, rhs: RHS) -> MatrixMul<Self, RHS>;
}

pub trait MatrixHStack<R> where Self: Sized
{
	unsafe fn unsafe_hstack(self, right: R) -> HStack<Self, R>;
	fn hstack(self, right: R) -> HStack<Self, R>;
}

pub trait MatrixVStack<B> where Self: Sized
{
	unsafe fn unsafe_vstack(self, bot: B) -> VStack<Self, B>;
	fn vstack(self, bot: B) -> VStack<Self, B>;
}

pub trait MatrixElems where Self: Sized
{
	fn elems(self) -> MatrixElements<Self>;
}

pub trait ToMatrix
{
	fn to_mat(self) -> Matrix;
}

impl<LHS: MatrixRawGet + MatrixShape,
     T: MatrixIndexGet<LHS>>
MatrixGet<T> for
LHS
{
	unsafe fn unsafe_get(&self, idx: T) -> f64
	{
		idx.unsafe_get_idx(self)
	}

	fn get(&self, idx: T) -> f64
	{
		idx.get_idx(self)
	}
}

impl<LHS: MatrixRawSet + MatrixShape,
     T: MatrixIndexSet<LHS>>
MatrixSet<T> for
LHS
{
	unsafe fn unsafe_set(&self, idx: T, val: f64)
	{
		idx.unsafe_set_idx(self, val);
	}

	fn set(&self, idx: T, val: f64)
	{
		idx.set_idx(self, val);
	}
}

//~ impl<LHS: MatrixShape + MatrixRawSet,
     //~ RHS: MatrixShape + MatrixRawGet>
impl<LHS: MatrixShape + MatrixRawSet + MatrixSet<usize>,
     RHS: MatrixShape + MatrixRawGet + MatrixGet<usize>>
MatrixAssign<RHS> for LHS
{
	unsafe fn unsafe_assign(&self, m: RHS)
	{
		//~ for r in 0..self.nrow()
		//~ {
			//~ for c in 0..self.ncol()
			//~ {
				//~ self.raw_set(r, c, m.raw_get(r, c));
			//~ }
		//~ }
		//~ 
		for i in 0..self.len()
		{
			self.unsafe_set(i, m.unsafe_get(i));
		}
	}
	
	fn assign(&self, m: RHS)
	{
		assert_eq!(self.nrow(), m.nrow());
		assert_eq!(self.ncol(), m.ncol());
		unsafe
		{
			self.unsafe_assign(m);
		}
	}
}

impl<T: MatrixShape + MatrixGet<usize>>
ToMatrix for T
{
	fn to_mat(self) -> Matrix
	{
		Matrix::from_iter(self.nrow(), self.ncol(), self.elems())
	}
}

// TODO: Re-add when the trait reform is implemented
//~ impl<T: MatrixShape>
//~ SameShape for
//~ T
//~ {
	//~ fn same_shape(&self, nrow: usize, ncol: usize) -> bool
	//~ {
		//~ self.nrow() == nrow && self.ncol() == ncol
	//~ }
//~ }