diff --git a/examples/Language_Fundamentals/usage_Matrices_and_Arrays.jl b/examples/Language_Fundamentals/usage_Matrices_and_Arrays.jl index 8008836..b11bba0 100644 --- a/examples/Language_Fundamentals/usage_Matrices_and_Arrays.jl +++ b/examples/Language_Fundamentals/usage_Matrices_and_Arrays.jl @@ -13,53 +13,80 @@ else end ################################################################ ################################################################ -mZeros0 = zerosM(:mat, 2) # same as zeros(2,2) +mzero0 = zerosM(:mat, 2) # same as zeros(2,2) -mZeros1 = zerosM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method +mzero1 = zerosM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method -mZeros2 = zerosM(2) # same as zeros(2) +A=[2 1 2] +mzero2 = zerosM(2, like=A) # same as zeros(Int64,2) -mZeros3 = zerosM(Int32, 2, 2) # same as zeros(Int32,2,2) +mzero3 = zerosM(2) # same as zeros(2) -mZeros4 = zerosM((2, 2)) # = zerosM(2,2) # giving size as Tuple +mzero4 = zerosM(Int32, 2, 2) # same as zeros(Int32,2,2) -mZeros5 = zerosM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type +# giving size as Tuple +mzero5 = zerosM((2, 2)) # = zerosM(2,2) -mZeros6 = zerosM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. +mzero6 = zerosM(Int32, (2, 2)) -mZeros7 = zerosM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way +# giving size as an Array +## non-efficient Matlab way. Array should be Integer otherwise you will get errors. + +mzero7 = zerosM([2, 2]) + +mzero8 = zerosM(Int32, [2, 2]) + +mzero9 = zerosM([2, 2], like = [2.5 3.0]) ################################################################ -mOnes0 = onesM(:mat, 2) # same as ones(2,2) +mone0 = onesM(:mat, 2) # same as ones(2,2) + +mone1 = onesM(:mat, 2, like = onesM(Int32, 2, 2)) # like method + +A=[2 1 2] +mone2 = onesM(2, like=A) # same as ones(Int64,2) -mOnes1 = onesM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method +mone3 = onesM(2) # same as ones(2) -mOnes2 = onesM(2) # same as ones(2) +mone4 = onesM(Int32, 2, 2) # same as ones(Int32,2,2) -mOnes3 = onesM(Int32, 2, 2) # same as ones(Int32,2,2) +# giving size as Tuple +mone5 = onesM((2, 2)) # = onesM(2,2) -mOnes4 = onesM((2, 2)) # = onesM(2,2) # giving size as Tuple +mone6 = onesM(Int32, (2, 2)) -mOnes5 = onesM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type +# giving size as an Array +## non-efficient Matlab way. Array should be Integer otherwise you will get errors. -mOnes6 = onesM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. +mone7 = onesM([2, 2]) -mOnes7 = onesM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way +mone8 = onesM(Int32, [2, 2]) + +mone9 = onesM([2, 2], like = [2.5 3.0]) ################################################################ -mRand0 = randM(:mat, 2) # same as rand(2,2) +mrandom0 = randM(:mat, 2) # same as rand(2,2) + +mrandom1 = randM(:mat, 2, like = randM(Int32, 2, 2)) # like method + +A=[2 1 2] +mrandom2 = randM(2, like=A) # same as rand(Int64,2) + +mrandom3 = randM(2) # same as rand(2) -mRand1 = randM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method +mrandom4 = randM(Int32, 2, 2) # same as rand(Int32,2,2) -mRand2 = randM(2) # same as rand(2) +# giving size as Tuple +mrandom5 = randM((2, 2)) # = randM(2,2) -mRand3 = randM(Int32, 2, 2) # same as rand(Int32,2,2) +mrandom6 = randM(Int32, (2, 2)) -mRand4 = randM((2, 2)) # = onesM(2,2) # giving size as Tuple +# giving size as an Array +## non-efficient Matlab way. Array should be Integer otherwise you will get errors. -mRand5 = randM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type +mrandom7 = randM([2, 2]) -mRand6 = randM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. +mrandom8 = randM(Int32, [2, 2]) -mRand7 = randM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way +mrandom9 = randM([2, 2], like = [2.5 3.0]) ################################################################ mTrue0 = trueM(:mat, 2) # same as trues(2,2) diff --git a/src/Language_Fundamentals/Matrices_and_Arrays.jl b/src/Language_Fundamentals/Matrices_and_Arrays.jl index 1a98757..2266a29 100644 --- a/src/Language_Fundamentals/Matrices_and_Arrays.jl +++ b/src/Language_Fundamentals/Matrices_and_Arrays.jl @@ -13,152 +13,108 @@ for n in natives end end ################################################################ -""" - zerosM(:mat, dim) # square dim*dim matrix - zerosM(:mat, Type,dim) # square dim*dim matrix - zerosM(:mat, dim, like=anArray) # to make an array with similar type of anArray - zerosM(sizeAsArray) # non-efficient Matlab way - zerosM(Type, sizeAsArray) # non-efficient Matlab way - zerosM(sizeAsArray, like=anArray) # to make an array with similar type of anArray - -returns an array filled with zero values. - -`zerosM(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument. - -# Examples -```julia -mZeros0 = zerosM(:mat, 2) # same as zeros(2,2) - -mZeros1 = zerosM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method - -mZeros2 = zerosM(2) # same as zeros(2) - -mZeros3 = zerosM(Int32, 2, 2) # same as zeros(Int32,2,2) - -mZeros4 = zerosM((2, 2)) # = zerosM(2,2) # giving size as Tuple - -mZeros5 = zerosM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type - -mZeros6 = zerosM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. +import Base: DimOrInd, OneTo +for (fname, fnative, docIn) in ((:zerosM, :zeros, "zero"), (:onesM, :ones, "one"), (:randM, :rand, "random")) + @eval begin -mZeros7 = zerosM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way -``` -""" -zerosM(args...) = zeros(args...) # includes T::Type method + # init + function $fname end -function zerosM(matSymbol::Symbol, T::Type, dim::Integer) - if matSymbol == :mat - return zeros(T, dim, dim) - end -end + # doc + @doc """ + $($fname) -function zerosM(matSymbol::Symbol, dim::Integer; like::AbstractArray = Vector{Float64}()) - if matSymbol == :mat - return zeros(eltype(like), dim, dim) - end -end + returns an array filled with $($docIn) values. -# zerosM(dim::Integer; like::Union{Array,Nothing}=nothing) = like === nothing ? zeros(dim,dim) : zeros(eltype(like), dim, dim) + In addition to original Julia methods the following methods are provided: -zerosM(T::Type, a::Array) = zeros(T, Tuple(a)) -zerosM(a::Array; like::AbstractArray = Vector{Float64}()) = - zeros(eltype(like), Tuple(a)) -################################################################ -""" - onesM(:mat, dim) # square dim*dim matrix - onesM(:mat, Type,dim) # square dim*dim matrix - onesM(:mat, dim, like=anArray) # to make an array with similar type of anArray - onesM(sizeAsArray) # non-efficient Matlab way - onesM(Type, sizeAsArray) # non-efficient Matlab way - onesM(sizeAsArray, like=anArray) # to make an array with similar type of anArray + $($fname)(..., like = anArray) # to make an array with similar type of anArray + $($fname)(Type, ...) # to give type explicitly -returns an array filled with one values. + Above input arguments work with any other method. -`onesM(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument. + $($fname)(sizeAsArray) -# Examples -```julia -mOnes0 = onesM(:mat, 2) # same as ones(2,2) + To give size as an array (non-efficient Matlab way). -mOnes1 = onesM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method + $($fname)(:mat, dim) # square dim*dim matrix + $($fname)(:mat, Type, dim) # square dim*dim matrix -mOnes2 = onesM(2) # same as ones(2) + `$($fname)(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument. -mOnes3 = onesM(Int32, 2, 2) # same as ones(Int32,2,2) + # Examples + ```julia + m$($docIn)0 = $($fname)(:mat, 2) # same as $($fnative)(2,2) -mOnes4 = onesM((2, 2)) # = onesM(2,2) # giving size as Tuple + m$($docIn)1 = $($fname)(:mat, 2, like = $($fname)(Int32, 2, 2)) # like method -mOnes5 = onesM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type + A=[2 1 2] + m$($docIn)2 = $($fname)(2, like=A) # same as $($fnative)(Int64,2) -mOnes6 = onesM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. + m$($docIn)3 = $($fname)(2) # same as $($fnative)(2) -mOnes7 = onesM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way -``` -""" -onesM(args...) = ones(args...) # includes T::Type method + m$($docIn)4 = $($fname)(Int32, 2, 2) # same as $($fnative)(Int32,2,2) -function onesM(matSymbol::Symbol, T::Type, dim::Integer) - if matSymbol == :mat - return ones(T, dim, dim) - end -end + # giving size as Tuple + m$($docIn)5 = $($fname)((2, 2)) # = $($fname)(2,2) -function onesM(matSymbol::Symbol, dim::Integer; like::AbstractArray = Vector{Float64}()) - if matSymbol == :mat - return ones(eltype(like), dim, dim) - end -end + m$($docIn)6 = $($fname)(Int32, (2, 2)) -# onesM(dim::Integer; like::Union{Array,Nothing}=nothing) = like === nothing ? ones(dim,dim) : ones(eltype(like), dim, dim) + # giving size as an Array + ## non-efficient Matlab way. Array should be Integer otherwise you will get errors. -onesM(T::Type, a::Array) = ones(T, Tuple(a)) -onesM(a::Array; like::AbstractArray = Vector{Float64}()) = - ones(eltype(like), Tuple(a)) -################################################################ -""" - randM(:mat, dim) # square dim*dim matrix - randM(:mat, Type, dim) # square dim*dim matrix - randM(:mat, dim, like = anArray) # to make an array with similar type of anArray - randM(sizeAsArray) # non-efficient Matlab way - randM(Type, sizeAsArray) # non-efficient Matlab way - randM(sizeAsArray, like = anArray) # to make an array with similar type of anArray + m$($docIn)7 = $($fname)([2, 2]) -returns an array filled with random values. + m$($docIn)8 = $($fname)(Int32, [2, 2]) -`randM(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument. + m$($docIn)9 = $($fname)([2, 2], like = [2.5 3.0]) + ``` + """ $(fname) -# Examples -```julia -mRand0 = randM(:mat, 2) # same as rand(2,2) + # copying methods + # includes T::Type methods + $fname(args...)=$fnative(args...) -mRand1 = randM(:mat, 2, like = zerosM(Int32, 2, 2)) # like method + # Tuple as input methods + $fname(dims::DimOrInd...; like::Array = Vector{Float64}()) = + $fnative(eltype(like), dims) -mRand2 = randM(2) # same as rand(2) + $fname(dims::NTuple{N, Union{Integer, OneTo}}; like::Array = Vector{Float64}()) where {N} = + $fnative(eltype(like), dims) -mRand3 = randM(Int32, 2, 2) # same as rand(Int32,2,2) + $fname(dims::Tuple{Vararg{DimOrInd}}; like::Array = Vector{Float64}()) = + $fnative(eltype(like), dims) -mRand4 = randM((2, 2)) # = randM(2,2) # giving size as Tuple + $fname(dims::Tuple{}; like::Array = Vector{Float64}()) = + $fnative(eltype(like), dims) -mRand5 = randM(Int32, (2, 2)) # giving size as Tuple and also the Int32 type + # another way kept as a reference + # $fname(dims::Tuple{}}; like::Union{Array,Nothing}=nothing) = + # like === nothing + # ? $fnative(dims) + # : $fnative(eltype(like), dims) -mRand6 = randM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors. + # Array as input methods + $fname(a::Array; like::Array = Vector{Float64}()) = + $fnative(eltype(like), Tuple(a)) -mRand7 = randM(Int32, [2, 2]) # giving size as Array, non-efficient Matlab way -``` -""" -randM(args...) = rand(args...) # includes T::Type method + $fname(::Type{T}, a::Array) where {T} = $fnative(T, Tuple(a)) -function randM(matSymbol::Symbol, T::Type, dim::Integer) - if matSymbol == :mat - return rand(T, dim, dim) - end -end + # square matrix with one dim input + function $fname(matSymbol::Symbol, ::Type{T}, dim::Integer) where {T} + if matSymbol == :mat + return $fnative(T, dim, dim) + end + end -function randM(matSymbol::Symbol, dim::Integer; like::AbstractArray = Vector{Float64}()) - if matSymbol == :mat - return rand(eltype(like), dim, dim) + function $fname(matSymbol::Symbol, dim::Integer; like::Array=Vector{Float64}()) + if matSymbol == :mat + return $fnative(eltype(like), dim, dim) + end + end end end +################################################################ # randM(dim::Integer; like::Union{Array,Nothing}=nothing) = like === nothing ? rand(dim,dim) : rand(eltype(like), dim, dim)