Skip to content

Commit

Permalink
starting work on SubArray. feel free to contribute.
Browse files Browse the repository at this point in the history
related to issue #93.
  • Loading branch information
JeffBezanson committed Jun 30, 2011
1 parent d4bdcaf commit e2cdcc7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions j/tensor.j
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,35 @@ function ind2sub(dims, ind::Index)
end
return sub
end

## subarrays ##

type SubArray{T,N,A<:Tensor,I<:(Indices...)} <: Tensor{T,N}
parent::A
indexes::I
dims::Dims

SubArray(p::A, i::I) = new(p, i, map(length, i))
end

sub{T,N}(A::Tensor{T,N}, i::NTuple{N,Indices}) =
SubArray{T,N,typeof(A),typeof(i)}(A, i)

sub(A::Tensor, i::Indices...) = sub(A, i)

size(s::SubArray) = s.dims
ndims{T,N}(s::SubArray{T,N}) = N

copy(s::SubArray) = copy_to(similar(s.parent, size(s)), s)
similar(s::SubArray, T::Type, dims::Dims) = similar(s.parent, T, dims)

ref(s::SubArray) = s

ref{T}(s::SubArray{T,1}, i::Index) = s.parent[s.indexes[1][i]]

ref{T}(s::SubArray{T,2}, i::Index, j::Index) =
s.parent[s.indexes[1][i], s.indexes[2][j]]

ref(s::SubArray, is::Index...) = s.parent[map(ref, s.indexes, is)...]

ref(s::SubArray, i::Index) = s[ind2sub(size(s), i)...]

0 comments on commit e2cdcc7

Please sign in to comment.