Skip to content

yoyoyoju/TensorMatrices_lemon.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TensorMatrices_lemon Module

# TensorMatrices_lemonModule.

convert tensor to matrix or matrix to tensor storing the index info

source

Types

# TensorMatrices_lemon.TenmatType.

Tenmat{T}(matrix::Array{T,2}, rowindex::Vector{Int}, colindex::Vector{Int}, tensorsize::Tuple{Vararg{Int}})

Matrix which stores the indices to convert it back to tensor.

Example

matrix = Array(reshape(1:48, 6, 8)) # some 6 x 8 matrix
rowindex = [3,1]
colindex = [2,4]
tensorsize = (2,4,3,2)
tenmat = Tenmat(matrix, rowindex, colindex, tensorsize)

# output

TensorMatrices_lemon.Tenmat{Int64}([1 7  37 43; 2 8  38 44;  ; 5 11  41 47; 6 12  42 48],[3,1],[2,4],(2,4,3,2))

source

# TensorMatrices_lemon.TenmatMethod.

Tenmat{T}(matrix::Matrix{T})

Set a matrix as a Tenmat.

source

# TensorMatrices_lemon.TenmatMethod.

Tenmat{T,N}(tensor::Array{T,N}, rowindex::Vector{Int}, colindex::Vector{Int})

Set a tensor as a Tenmat.

Tenmat.matrix will return the converted matrix.

Example

A = Array(reshape(1:3024, 4,7,2,9,6)) # tensor, size of [4,7,2,9,6]
rowindex = [5,2] # 5th and 2nd indices maps to the row
colindex = [3,4,1] # 3rd, 4th and 1st indices maps to the column
tenmat = Tenmat(A,rowindex,colindex)

# output

TensorMatrices_lemon.Tenmat{Int64}([1 29  452 480; 505 533  956 984;  ; 2041 2069  2492 2520; 2545 2573  2996 3024],[5,2],[3,4,1],(4,7,2,9,6))

source

Methods

what to write: size - DONE getindex - DONE setindex! - DONE similar - DONE ndims - DONE getMatrix - DONE tensorSize - DONE –– DO THE EXAMPLES!!! tensor2tenmat - DONE tenmat2tensor - DONE

# Base.sizeMethod.

size(tenmat::Tenmat, args...)

Return the size of the tenmat.matrix.

julia> size(tenmat)
(42,72)

julia> size(tenmat,2)
72

source

# Base.setindex!Method.

setindex!(tenmat::Tenmat, args...)

Expands setindex! by tenmat.matrix.

julia> setindex!(tenmat, 100, 2, 6);

source

# Base.getindexMethod.

getindex(tenmat::Tenmat, args...)

Expands getindex by tenmat.matrix.

julia> getindex(tenmat, 2, 6)
100

source

# Base.similarMethod.

similar

similar

Examples

A = rand(4,7,6)
tenmat = Tenmat(A,[3,1],[2])
similar(tenmat, Int)
similar(tenmat, Int, (5,2,7))
similar(tenmat, Int, [1,3],[2])
similar(tenmat, Int, [4,2,5], [1,3], (2,4,3,1,6))
similar(tenmat)
similar(tenmat, (5,2,7))
similar(tenmat, [1,3],[2])
B = similar(tenmat, [4,2,5], [1,3], (2,4,3,1,6))
size(B)

# output

(24,6)

source

# Base.ndimsMethod.

ndims(tenmat::Tenmat)

Return the original dimension of tenmat, from tensorsize.

A = Array(reshape(1:3024, 4,7,2,9,6)) 
tenmat = Tenmat(A,[5,2],[3,4,1])
ndims(tenmat)

# output

5

source

# TensorMatrices_lemon.tensorSizeMethod.

tensorSize(tenmat::Tenmat)

Return the tensorsize.

julia> tensorSize(tenmat)
(4,7,2,9,6)

source

# TensorMatrices_lemon.getMatrixMethod.

getMatrix(tenmat::Tenmat)

Return the matrix.

A = Array(reshape(1:18, 3, 2, 3))
tenmat = Tenmat(A,[3,1],[2])
getMatrix(tenmat)

# output

9×2 Array{Int64,2}:
  1   4
  7  10
 13  16
  2   5
  8  11
 14  17
  3   6
  9  12
 15  18

source

# TensorMatrices_lemon.tensor2tenmatMethod.

tensor2tenmat{T,N}(tensor::Array{T,N}, rowindex::Vector{Int}, colindex::Vector{Int})

Convert tensor to tenmat.

examples

julia> A = rand(3,5,3,2,6);

julia> tenmatA = tensor2tenmat(A,[3,5,2],[4,1]);

julia> size(getMatrix(tenmatA))
(90,6)

julia> tensorSize(tenmatA)
(3,5,3,2,6)


julia> B = Array(reshape(1:10,2,5));

julia> tenmatB = tensor2tenmat(B,[2],[1]);

julia> tenmatB.matrix == transpose(B)
true

julia> tensorSize(tenmatB)
(2,5)

source

# TensorMatrices_lemon.tenmat2tensorMethod.

tenmat2tensor{T}(tenmat::Tenmat{T})

Convert tenmat back to the tensor.

examples

julia> A = rand(3,5,3,2,8);

julia> tenmatA = tensor2tenmat(A,[3,5,2],[4,1]);

julia> back2A = tenmat2tensor(tenmatA);

julia> A == back2A
true
B = rand(8,6,7,5,8,5,3,8)
tenmatB = tensor2tenmat(B,[1,7,3,2,6],[5,8,4])
back2B = tenmat2tensor(tenmatB)
check = true
for a=1:8, b=1:6, c=1:7, d=1:5, e=1:8, f=1:5, g=1:3, h=1:8
	back2B[a,b,c,d,e,f,g,h] == B[a,b,c,d,e,f,g,h] || check * false
end
check

# output

true

source

index

About

julia package for tensor matrix module

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages