ReduceLinAlg.jl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. __precompile__()
  2. module ReduceLinAlg
  3. using Reduce, LinearAlgebra
  4. # This file is part of ReduceLinAlg.jl. It is licensed under the MIT license
  5. # Copyright (C) 2018 Michael Reed
  6. const VectorAny = Union{Vector,Adjoint}
  7. const lin = [
  8. :hessian,
  9. :mat_jacobian,
  10. :band_matrix,
  11. :block_matrix,
  12. :char_matrix,
  13. :char_poly,
  14. :diagonal,
  15. :extend,
  16. :gram_schmidt,
  17. :hilbert,
  18. :jordan_block,
  19. :kronecker_product,
  20. ]
  21. const unr = [
  22. :cholesky,
  23. :coeff_matrix,
  24. :hermitian_tp,
  25. :symmetricp,
  26. :toeplitz,
  27. :triang_adjoint,
  28. :vandermonde,
  29. ]
  30. :(export $([lin;unr]...)) |> eval
  31. for fun in lin
  32. @eval begin
  33. $(Reduce.parsegen(fun,:args))
  34. $(Reduce.unfoldgen(fun,:args))
  35. function $fun(expr::String,s...;be=0)
  36. convert(String, $fun(RExpr(expr),s...;be=be))
  37. end
  38. end
  39. end
  40. for fun in unr
  41. @eval begin
  42. $(Reduce.parsegen(fun,:unary))
  43. $(Reduce.unfoldgen(fun,:unary))
  44. function $fun(expr::String;be=0)
  45. convert(String, $fun(RExpr(expr);be=be))
  46. end
  47. end
  48. end
  49. const MatExpr = Union{Array{Any,2},Array{Expr,2},Array{Symbol,2},Expr,Symbol}
  50. band_matrix(r::Union{Vector,Adjoint,Expr,Symbol},v::Integer) = band_matrix(list(r),v) |> parse
  51. band_matrix(r::T,v::Integer) where T <: Tuple = band_matrix(RExpr(r),v) |> parse
  52. block_matrix(r::Integer,c::Integer,s::VectorAny) = block_matrix(RExpr(r),RExpr(c),list(s)) |> parse
  53. block_matrix(r::Integer,c::Integer,s::T) where T <: Tuple = block_matrix(RExpr(r),RExpr(c),RExpr(s)) |> parse
  54. char_matrix(r::MatExpr,v::Any) = mat(char_matrix(list(r),RExpr(v)) |> parse,r)
  55. char_poly(r::MatExpr,v::Any) = mat(char_matrix(list(r),RExpr(v)) |> parse,r)
  56. extend(a::MatExpr,r::Integer,c::Integer,s) = mat(extend(RExpr(a),r,c,RExpr(s)) |> parse,a)
  57. hessian(r::Reduce.ExprSymbol,l::T) where T <: VectorAny = hessian(r,list(l))
  58. hessian(r::Reduce.ExprSymbol,l::T) where T <: Tuple = hessian(r,RExpr(l))
  59. @doc """
  60. hessian(expr,var_list::Vector)
  61. Computes the Hessian matrix of `expr` with respect to the variables in `var_list`.
  62. 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])`.
  63. """ hessian
  64. hilbert(a::Integer,r) = hilbert(RExpr(a),RExpr(r)) |> parse
  65. function mat_jacobian(r::T,v::S) where T <: VectorAny where S <: VectorAny
  66. mat_jacobian(list(r),list(v)) |> parse
  67. end
  68. function mat_jacobian(r::T,v::S) where T <: Tuple where S <: Tuple
  69. mat_jacobian(RExpr(r),RExpr(v)) |> parse
  70. end
  71. @doc """
  72. mat_jacobian(expr_list::Vector,var_list::Vector)
  73. Computes the Jacobian matrix of `expr_list` with respect to `var_list`.
  74. 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.
  75. """ mat_jacobian
  76. export jacobian
  77. jacobian = mat_jacobian
  78. @doc """
  79. jordan_block(expr,square_size::Integer)
  80. Computes the square Jordan block matrix `J` of dimension `square_size`.
  81. 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.
  82. """ jordan_block
  83. kronecker_product(a::MatExpr,b::MatExpr) = mat(kronecker(RExpr(a),RExpr(b)) |> parse,a,b)
  84. cholesky(r::Array{T,2}) where T <: Number = cholesky(RExpr(r)) |> parse |> mat
  85. coeff_matrix(r::VectorAny) = coeff_matrix(list(r)) |> parse
  86. coeff_matrix(r::T) where T <: Tuple = coeff_matrix(RExpr(r)) |> parse
  87. diagonal(r::VectorAny) = diagonal(list(r)) |> parse
  88. diagonal(r::T) where T <: Tuple = diagonal(RExpr(r)) |> parse
  89. gram_schmidt(r::Vector{<:Vector}) = gram_schmidt(list(r)) |> parse
  90. gram_schmidt(r::T) where T <: Tuple = gram_schmidt(RExpr(r)) |> parse
  91. gram_schmidt(r::Matrix) = gram_schmidt(list(r)) |> parse
  92. hermitian_tp(r::MatExpr) = mat(hermitian_tp(RExpr(r)) |> parse, r)
  93. symmetricp(r::MatExpr) = mat(symmetricp(RExpr(r)) |> parse, r)
  94. toeplitz(r::VectorAny) = toeplitz(list(r)) |> parse
  95. toeplitz(r::T) where T <: Tuple = toeplitz(RExpr(r)) |> parse
  96. triang_adjoint(r::MatExpr) = mat(triang_adjoint(RExpr(r)) |> parse, r)
  97. vandermonde(r::VectorAny) = vandermonde(list(r)) |> parse
  98. vandermonde(r::T) where T <: Tuple = vandermonde(RExpr(r)) |> parse
  99. function __init__()
  100. load_package(:linalg)
  101. end
  102. end # module