Skip to content

Commit

Permalink
metaprogramming for trueM, falseM
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Oct 15, 2019
1 parent 0bd1469 commit 5f391ab
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 54 deletions.
22 changes: 14 additions & 8 deletions examples/Language_Fundamentals/usage_Matrices_and_Arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,27 @@ mrandom8 = randM(Int32, [2, 2])

mrandom9 = randM([2, 2], like = [2.5 3.0])
################################################################
mTrue0 = trueM(:mat, 2) # same as trues(2,2)
mtrue0 = trueM(:mat, 2) # same as trues(2,2)

mTrue1 = trueM(2) # same as trues(2)
mtrue1 = trueM(2) # same as trues(2)

mTrue2 = trueM((2, 2)) # = onesM(2,2) # giving size as Tuple
# giving size as Tuple
mtrue2 = trueM((2, 2)) # = trues(2,2)

mTrue3 = trueM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors.
# giving size as an Array
## non-efficient Matlab way. Array should be Integer otherwise you will get errors.
mtrue3 = trueM([2, 2])
################################################################
mFalse0 = falseM(:mat, 2) # same as falses(2,2)
mfalse0 = falseM(:mat, 2) # same as falses(2,2)

mFalse1 = falseM(2) # same as falses(2)
mfalse1 = falseM(2) # same as falses(2)

mFalse2 = falseM((2, 2)) # = falseM(2,2) # giving size as Tuple
# giving size as Tuple
mfalse2 = falseM((2, 2)) # = falses(2,2)

mFalse3 = falseM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors.
# giving size as an Array
## non-efficient Matlab way. Array should be Integer otherwise you will get errors.
mfalse3 = falseM([2, 2])
################################################################
mEye0 = eyeM(2); # [1 0 0; 0 1 0]

Expand Down
79 changes: 34 additions & 45 deletions src/Language_Fundamentals/Matrices_and_Arrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,69 +115,58 @@ for (fname, fnative, docIn) in ((:zerosM, :zeros, "zero"), (:onesM, :ones, "one"
end
end
################################################################
for (fname, fnative, docIn) in ((:trueM, :trues, "true"), (:falseM, :falses, "false"))
@eval begin

# randM(dim::Integer; like::Union{Array,Nothing}=nothing) = like === nothing ? rand(dim,dim) : rand(eltype(like), dim, dim)

randM(T::Type, a::Array) = rand(T, Tuple(a))
randM(a::Array; like::AbstractArray = Vector{Float64}()) =
rand(eltype(like), Tuple(a))
# init
function $fname end

################################################################
"""
trueM(:mat, dim) # square dim*dim matrix
trueM(sizeAsArray) # non-efficient Matlab way
# doc
@doc """
$($fname)
returns an array filled with true values.
returns an array filled with $($docIn) values.
`trueM(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument.
In addition to original Julia methods the following methods are provided:
# Examples
```julia
mTrue0 = trueM(:mat, 2) # same as trues(2,2)
$($fname)(sizeAsArray)
mTrue1 = trueM(2) # same as trues(2)
To give size as an array (non-efficient Matlab way).
mTrue2 = trueM((2, 2)) # = trues(2,2) # giving size as Tuple
$($fname)(:mat, dim) # square dim*dim matrix
mTrue3 = trueM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors.
```
"""
trueM(args...) = trues(args...) # includes T::Type method
`$($fname)(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument.
function trueM(matSymbol::Symbol, dim::Integer)
if matSymbol == :mat
return trues(dim, dim)
end
end
trueM(a::Array) = trues(Tuple(a))
################################################################
"""
falseM(:mat, dim) # square dim*dim matrix
falseM(sizeAsArray) # non-efficient Matlab way
returns an array filled with false values.
# Examples
```julia
m$($docIn)0 = $($fname)(:mat, 2) # same as $($fnative)(2,2)
`falseM(dim)` returns 1-dimensional array. To get a square matrix like in Matlab, pass `:mat` as the 1st argument.
m$($docIn)1 = $($fname)(2) # same as $($fnative)(2)
# Examples
```julia
mFalse0 = falseM(:mat, 2) # same as falses(2,2)
# giving size as Tuple
m$($docIn)2 = $($fname)((2, 2)) # = $($fnative)(2,2)
mFalse1 = falseM(2) # same as falses(2)
# giving size as an Array
## non-efficient Matlab way. Array should be Integer otherwise you will get errors.
m$($docIn)3 = $($fname)([2, 2])
```
""" $(fname)

mFalse2 = falseM((2, 2)) # = falseM(2,2) # giving size as Tuple
# copying methods
$fname(args...)=$fnative(args...)

mFalse3 = falseM([2, 2]) # giving size as an Array, non-efficient Matlab way. Array should be Integer otherwise you will get errors.
```
"""
falseM(args...) = falses(args...) # includes T::Type method
# Array as input methods
$fname(a::Array) =$fnative(Tuple(a))

function falseM(matSymbol::Symbol, dim::Integer)
if matSymbol == :mat
return falses(dim, dim)
# square matrix with one dim input
function $fname(matSymbol::Symbol, dim::Integer)
if matSymbol == :mat
return $fnative(dim, dim)
end
end
end
end
falseM(a::Array) = falses(Tuple(a))
################################################################
"""
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ else
using Test
end
################################################################
# These tests don't test every single method. For more examples, refer to Examples folder.
# These tests don't test every single method. For more examples, refer to examples folder.

@testset "MatLang.j - Entering_Commands" begin

Expand Down

2 comments on commit 5f391ab

@aminya
Copy link
Member Author

@aminya aminya commented on 5f391ab Oct 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/4220

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.0 -m "<description of version>" 5f391ab3a0f5e0846d8bfdc1afc6e45685ec412d
git push origin v0.1.0

Please sign in to comment.