Math 无限矩阵的重写

Math 无限矩阵的重写,math,matrix,julia,symbolic-math,Math,Matrix,Julia,Symbolic Math,我参加了一个名为“符号线性代数”的项目,该项目是关于对无限矩阵进行基本运算,如加法、乘法、访问特定元素等。我将在Julia上实现这些运算 对于指定这些无限矩阵,我们将有一些数学案例,如: 因此,矩阵的视觉表示将如下所示: 例如,假设我们想为这个例子找到A+A。在这里,我们的案例发生了变化,所以我们需要重写这些案例以获得所需的输出?我知道Mathematica会这么做,但我该如何实现呢?是的,这太笼统了,所以让我问一些问题 让我们从案例作为输入开始。可能有许多不同规则的情况,例如,如果i%2

我参加了一个名为“符号线性代数”的项目,该项目是关于对无限矩阵进行基本运算,如加法、乘法、访问特定元素等。我将在Julia上实现这些运算

对于指定这些无限矩阵,我们将有一些数学案例,如:

因此,矩阵的视觉表示将如下所示:

例如,假设我们想为这个例子找到A+A。在这里,我们的案例发生了变化,所以我们需要重写这些案例以获得所需的输出?我知道Mathematica会这么做,但我该如何实现呢?是的,这太笼统了,所以让我问一些问题

  • 让我们从案例作为输入开始。可能有许多不同规则的情况,例如,如果i%2==0或i==j(如本例中所示),如何提供通用输入
  • 假设我已经完成了输入,我想做一些简单的操作。如何将这些情况组合到像Julia这样的编程语言中
我已经写了一些非通用的哑巴代码来看看事情会如何发展,所以我将提供我的代码来应用最小的可重复性示例,但不要把它当回事,我想我只是在寻找一个线索或路线图来消除我脑海中的问号

 using Parameters



struct inf_matrix
   mod_of :: Integer
   mod_value :: Integer
   i_coefficient :: Integer
   j_coefficient :: Integer
   value :: Integer
end


function single_demo(_mod_of :: Integer, _mod_value :: Integer, _i_coefficient :: Integer, _j_coefficient :: Integer, _value :: Integer)
   test_matrix = inf_matrix(_mod_of, _mod_value, _i_coefficient, _j_coefficient, _value)
   return test_matrix
end

function get_elem(st::inf_matrix ,i :: Integer, j :: Integer)
   #This function is not completed yet  
   if (i % st.mod_of == st.mod_value) && (2 * st.i_coefficient == j)
         return st.value;
   else
      return -1
   end
   

end

demo_1 = single_demo(2, 0 ,1, 2, 1)

println(get_elem(demo_1, 1, 0))

任何帮助都将不胜感激。

以下是您可以做到这一点的方法

import Base: getindex, +, *

abstract type InfiniteMatrix end

struct InfiniteIdentity <: InfiniteMatrix end

getindex(::InfiniteIdentity, i, j) = i .== j'

struct InfiniteConstant <: InfiniteMatrix 
    c
end

getindex(m::InfiniteConstant, i::Integer, j::Integer) = m.c
getindex(m::InfiniteConstant, i, j) = fill(m.c, size(i)..., size(j)...)

struct InfiniteMatrixFilter <: InfiniteMatrix
    condition::Function
    iftrue::InfiniteMatrix
    iffalse::InfiniteMatrix
end

getindex(m::InfiniteMatrixFilter, i, j) = ifelse.(m.condition.(i,j'), m.iftrue[i,j], m.iffalse[i,j]) 


struct InfiniteMatrixFunction <: InfiniteMatrix
    f::Function
    args
end

getindex(m::InfiniteMatrixFunction, i, j) = m.f(getindex.(m.args, Ref(i), Ref(j))...)

+(m1::InfiniteMatrix, m2::InfiniteMatrix) = InfiniteMatrixFunction(+, (m1, m2))
*(n::Number, m::InfiniteMatrix) = InfiniteMatrixFunction(x -> n*x, (m,))


(这只是一个概念验证,没有经过优化或无错误)

您正在寻找此软件包吗?使用它,您的第一个示例可以如下所示:
使用InfiniteArrays;A=眼睛(∞); A+A'
我可以用这个,谢谢。@rashid
julia> i = InfiniteIdentity()
InfiniteIdentity()

julia> c1 = InfiniteConstant(1)
InfiniteConstant(1)

julia> (2i+3c1)[1:5, 1:5]
5×5 Array{Int64,2}:
 5  3  3  3  3
 3  5  3  3  3
 3  3  5  3  3
 3  3  3  5  3
 3  3  3  3  5

julia> m = InfiniteMatrixFilter((i,j) -> i%2 == 0, c1, 0c1)
InfiniteMatrixFilter(var"#43#44"(), InfiniteConstant(1), InfiniteMatrixFunction(var"#41#42"{Int64}(0), (InfiniteConstant(1),)))

julia> m[1:5, 1:5]
5×5 Array{Int64,2}:
 0  0  0  0  0
 1  1  1  1  1
 0  0  0  0  0
 1  1  1  1  1
 0  0  0  0  0