Julia 中是否有任何函数可以实现这一点?
f(x, n) = vcat(repeat(x, div(n, length(x))), x[1:rem(n, length(x))])
f([1,3,2], 10)
10-element Vector{Int64}:
1
3
2
1
3
2
1
3
2
1
我想知道是否存在一个已知的函数可以做同样的事情。
Julia 中是否有任何函数可以实现这一点?
f(x, n) = vcat(repeat(x, div(n, length(x))), x[1:rem(n, length(x))])
f([1,3,2], 10)
10-element Vector{Int64}:
1
3
2
1
3
2
1
3
2
1
我想知道是否存在一个已知的函数可以做同样的事情。
a >= b && c <= b vs !( a < b || c > b)
julia 中哪个性能更高?
我正在尝试对 Julia 中的矩阵执行高斯消除,然后以分数形式而不是小数形式提取解决方案。例如,我想要 3/4 而不是 0.75 作为解决方案。
我当前用于减少矩阵行并将解决方案表示为小数的代码如下:
using RowEchelon
A = [1 2 3;
4 5 6;
7 8 10]
A_reduced rref(A)
print(A_reduced)
有没有办法修改此代码以获得分数而不是浮点解?
我正在尝试构建并求解一个如下所示的矩阵系统:
\begin{equation}
\begin{bmatrix}
B & D & 0 & \cdots & & 0 \\
A & B & D & \ddots & & \vdots \\
0 & A & B & D & \ddots & \\
\vdots & \ddots & & \ddots & \\
\vdots & \cdots & \cdots & A & B & D \\
0 & \cdots & \cdots & 0 & A & B
\end{bmatrix}
\
\begin{bmatrix}
\Delta c
\end{bmatrix}
\
=
\
\begin{bmatrix}
G
\end{bmatrix}
\end{equation}
该系统来自用于求解方程组 (PDE) 的一维有限体积方法。
大矩阵 ( BBM
) 由数组构造而成A, B, D
。以下代码可以正常运行,但我有兴趣使该代码的计算效率更高。
我很好奇堆栈溢出社区是否有任何建议。特别是有没有一种方法可以消除for
函数内的循环?
using BlockBandedMatrices
function calculate_BBM(; A, B, D)
# initialize the Block Banded Matrix (BBM)
int_array = map(Int, N*ones(NJ));
BBM = BlockBandedMatrix(Zeros{Float64}(N*NJ, N*NJ), int_array, int_array, (1,1))
# Precompute indices to avoid calculating them in each iteration
ind_begin = [(1 + N * (j - 1)) for j in 1:NJ]
ind_end = [N * j for j in 1:NJ]
for j in 2:NJ - 1
# Use precomputed indices
ib = ind_begin[j]
ie = ind_end[j]
BBM[ib:ie, (ib - N):(ie - N)] .= A[:, :, j - 1]
BBM[ib:ie, ib:ie] .= B[:, :, j]
BBM[ib:ie, (ib + N):(ie + N)] .= D[:, :, j]
end
j = 1
ib = ind_begin[j]
ie = ind_end[j]
BBM[ib:ie, ib:ie] .= B[:, :, j]
BBM[ib:ie, (ib + N):(ie + N)] .= D[:, :, j]
j = NJ
ib = ind_begin[j]
ie = ind_end[j]
BBM[ib:ie, (ib - N):(ie - N)] .= A[:, :, j - 1]
BBM[ib:ie, ib:ie] .= B[:, :, j]
return BBM
end
N = 3
NJ = 102
A = randn(N, N, NJ-1)
B = randn(N, N, NJ)
D = randn(N, N, NJ-1)
# A = -ones(N, N, NJ-1)
# B = ones(N, N, NJ) * 2
# D = ones(N, N, NJ-1)
BBM = calculate_BBM(A=A, B=B, D=D);
我正在努力用字符串中的双精度数替换数字。我怎样才能做到这一点?
这些都不适合我(我使用的是 Julia v1.8):
replace("hello num12 text and num5 numbers",r"num([0-9]+)"=>2*s"\1")
replace("hello num12 text and num5 numbers",r"num([0-9]+)"=>2*parse(Int,s"\1"))
replace("hello num12 text and num5 numbers",r"num([0-9]+)"=>2*parse(Int,SubstitutionString("\\1")))
输入:
“你好 num12 文本和 num5 数字”
预期输出(删除字符串“num”并将数字替换为其双精度):
“你好 24 条文字和 10 个数字”
任何帮助将不胜感激。
编辑:错别字。
我希望能够在数据框中给定开始日期和结束日期创建日期向量。但是,我收到以下错误:“无法将convert
Int64 类型的对象转换为 Day 类型的对象”
using DataFrames, Tidier, TidierDates
df = DataFrame(date_start = "2019-01-01", date_end = "2019-01-31")
describe(df)
df[!, :date_start] = TidierDates.ymd.(df[!, :date_start])
df[!, :date_end] = TidierDates.ymd.(df[!, :date_end])
describe(df)
df[1,:date_start]:df[1,:date_end]
我认为这样的事情可能会起作用,但没有:
date_vec = Dates.value(df[1,:date_start]):1:Dates.value(df[1,:date_end]) |> collect
date_days = Dates.Day(date_vec)
如何在 Julia 中加入字典,类似于 Python 的一种方式:
diction = {"DC": 4,
"NH": 1,
"MI": 36,
"MO": 4989,
"FL": 157,
"UT": 13,
"NV": 9,
"CA": 110,
"WV": 6,
"NY": 39,
"ME": 1,
"SC": 26,
"KY": 71}
stri = ' | '.join(map(lambda x: '%2.2s %5.5s' % (str(x[0]), str(x[1])), diction.items()))
我想出了什么:
diction = Dict(["DC" => 4
"NH" => 1,
"MI" => 36,
"MO" => 4989,
"FL" => 157,
"UT" => 13,
"NV" => 9,
"CA" => 110,
"WV" => 6,
"NY" => 39,
"ME" => 1,
"SC" => 26,
"KY" => 71])
stri = [@sprintf("%2.2s %5.5s | ",st,ct) for st,ct in keys(diction),values(diction)]
产量:
ERROR: syntax: invalid iteration specification
输出:
DC 4 | NH 1 | MI 36 | MO 4989 | FL 157 | UT 13 | NV 9 | CA 110 | WV 6 | NY 39 | ME 1 | SC 26 | KY 71
我正在按照 GitHub Copilot 代码编写一个函数,该函数获取一个矩阵,并根据时间在矩阵的每一行上拟合单变量线性回归,我遇到了一个奇怪的过程:
function fᵢ(adj_price::Matrix{Float64})
n_assets, n_days = size(adj_price)
n_days > 1 || ArgumentError("") |> throw
t = 1:n_days
# Fit a linear regression on each row of the adjusted price \
# matrix against t and store the gradients in aᵢ Vector.
aᵢ = zeros(n_assets)
for i in 1:n_assets
aᵢ[i] = adj_price[i, :] \ t
end
return aᵢ
end
重点是将每个回归模型的梯度存储在一个 Vector 中,并将该 Vector 作为最终输出返回。因此,应该针对时间变量对矩阵的每一行进行回归,以获得其系数(梯度)。我的问题在于我们有什么adj_price[i, :] \ t
。这个小运算能达到系数吗?