Skip to content

Commit

Permalink
added matrix constructor methods and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Apr 29, 2018
1 parent a656e62 commit ff102c0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# ReduceLinAlg.jl

[![Build Status](https://travis-ci.org/chakravala/ReduceLinAlg.jl.svg?branch=master)](https://travis-ci.org/chakravala/ReduceLinAlg.jl)

[![Coverage Status](https://coveralls.io/repos/chakravala/ReduceLinAlg.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/chakravala/ReduceLinAlg.jl?branch=master)

[![codecov.io](http://codecov.io/github/chakravala/ReduceLinAlg.jl/coverage.svg?branch=master)](http://codecov.io/github/chakravala/ReduceLinAlg.jl?branch=master)

**LINALG: Linear algebra package**
*A selection of functions that are useful in the world of linear algebra*

REDUCE package by Matt Rebbeck.

This Julia package builds on [Reduce.jl](https://github.com/chakravala/Reduce.jl) by dispatching methods from the "linalg" package.
84 changes: 79 additions & 5 deletions src/ReduceLinAlg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,30 @@ import Compat.String
# Copyright (C) 2018 Michael Reed

const VectorAny = Union{Vector,RowVector}
import Reduce: parse_calculus, treecombine!, irr

const lin = [
:hessian,
:mat_jacobian
:mat_jacobian,
:band_matrix,
:block_matrix,
:char_matrix,
:char_poly,
:diagonal,
:extend,
:gram_schmidt,
:hilbert,
:jordan_block,
:kronecker_product,
]

const unr = [
:cholesky,
:coeff_matrix,
:hermitian_tp,
:symmetricp,
:toeplitz,
:triang_adjoint,
:vandermonde,
]

:(export $(lin...)) |> eval
Expand All @@ -21,25 +40,80 @@ for fun in lin
@eval begin
$(Reduce.parsegen(fun,:calculus))
$(Reduce.unfoldgen(fun,:calculus))
function $fun(expr::Compat.String,s...;be=0)
convert(Compat.String, $fun(RExpr(expr),s...;be=be))
end
end
end

for fun in lin
for fun in unr
@eval begin
function $fun(expr::Compat.String,s...;be=0)
convert(Compat.String, $fun(RExpr(expr),s...;be=be))
$(Reduce.parsegen(fun,:unary))
$(Reduce.unfoldgen(fun,:unary))
function $fun(expr::Compat.String;be=0)
convert(Compat.String, $fun(RExpr(expr);be=be))
end
end
end

const MatExpr = Union{Array{Any,2},Array{Expr,2},Array{Symbol,2},Expr,Symbol}

band_matrix(r::Union{Vector,RowVector,Expr,Symbol},v::Integer) = band_matrix(list(r),v) |> parse
block_matrix(r::Integer,c::Integer,s::VectorAny) = block_matrix(RExpr(r),RExpr(c),list(s)) |> parse

char_matrix(r::Reduce.MatExpr,v::Any) = char_matrix(list(r),RExpr(v)) |> parse
char_poly(r::Reduce.MatExpr,v::Any) = char_matrix(list(r),RExpr(v)) |> parse

extend(a::MatExpr,r::Integer,c::Integer,s) = extend(RExpr(a),r,c,RExpr(s)) |> parse

hessian(r::Reduce.ExprSymbol,l::T) where T <: VectorAny = hessian(r,list(l))

@doc """
hessian(expr,var_list::Vector)
Computes the Hessian matrix of `expr` with respect to the variables in `var_list`.
This is an n×n matrix where n is the number of variables and the (i,j)th entry is `df(expr,var_list[i],var_list[j])`.
""" hessian

hilbert(a::Integer,r) = hilbert(RExpr(a),RExpr(r)) |> parse

function mat_jacobian(r::T,v::S) where T <: VectorAny where S <: VectorAny
mat_jacobian(list(r),list(v)) |> parse
end

@doc """
mat_jacobian(expr_list::Vector,var_list::Vector)
Computes the Jacobian matrix of `expr_list` with respect to `var_list`.
This is a matrix whose (i,j)th entry is `df(expr_list[i],var_list[j])`. The matrix is n×m where n is the number of variables and m is the number of expressions.
""" mat_jacobian

jacobian = mat_jacobian

jordan_block(r::Any,s::Integer) = jordan_block(RExpr(r),s) |> parse

@doc """
jordan_block(expr,square_size::Integer)
Computes the square Jordan block matrix `J` of dimension `square_size`.
The entries of `J` are `J[i,i] = expr` for i = 1,...,n, `J[i,i+1] = 1` for i = 1,...,n-1, and all other entries are 0.
""" jordan_block

kronecker_product(a::MatExpr,b::MatExpr) = kronecker(RExpr(a),RExpr(b)) |> parse

cholesky(r::Array{T,2}) where T <: Number = cholesky(RExpr(r)) |> parse |> mat
coeff_matrix(r::VectorAny) = coeff_matrix(list(r)) |> parse
diagonal(r::VectorAny) = diagonal(list(r)) |> parse
gram_shmidt(r::Vector{<:Vector}) = gram_shmidt(list(r)) |> parse
hermitian_tp(r::MatExpr) = hermitian_tp(RExpr(r)) |> parse
symmetricp(r::MatExpr) = symmetricp(RExpr(r)) |> parse
toeplitz(r::VectorAny) = toeplitz(list(r)) |> parse
triang_adjoint(r::MatExpr) = triang_adjoint(RExpr(r)) |> parse
vandermonde(r::VectorAny) = vandermonde(list(r)) |> parse

function __init__()
load_package(:linalg)
end
Expand Down

0 comments on commit ff102c0

Please sign in to comment.