小编JKH*_*KHA的帖子

在 Julia 变量中使用素数“'”

我有很多变量和矩阵,我想用素数来调用,例如:

\n

xprime, yprime, rprime等等。

\n

我相信我不能使用x', y', r'等,因为它例如用于矩阵转置。

\n
julia> x' = 5\nERROR: syntax: invalid assignment location "x'" around REPL[2]:1\nStacktrace:\n [1] top-level scope at REPL[2]:1\n\njulia> x = [1,2]\n2-element Array{Int64,1}:\n 1\n 2\n\njulia> x'\n1\xc3\x972 LinearAlgebra.Adjoint{Int64,Array{Int64,1}}:\n 1  2\n
Run Code Online (Sandbox Code Playgroud)\n

我愿意接受任何比 xprime、yprime 等更方便的方法的建议,我认为这是相当长和难看的。

\n

variables julia

8
推荐指数
1
解决办法
599
查看次数

.ipynb Julia 文件到 .jl 文件

有没有一种便捷的方法可以将 Jupyter Notebook 生成的多个 .ipynb Julia 文件转换为 .jl 工作文件?

如果保留用 Jupyter 编写的 markdown 是很好的,但对于我的使用来说不是强制的。

以下是我的文件包含的内容示例:

{
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Some useful functions required to build the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "distance (generic function with 1 method)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Euclidean distances between point p1 and p2\n",
    "function distance(p1,p2)\n",
    "    dist=((p1[1]-p2[1])^2+(p1[2]-p2[2])^2)^(1/2)\n",
    "    return dist\n",
    "end" …
Run Code Online (Sandbox Code Playgroud)

file file-conversion julia jupyter-notebook

6
推荐指数
2
解决办法
2147
查看次数

Julia GraphPlot 中有两个(或更多)节点标签集,可能使用 Compose?

以下是 Julia Discourse 的最小工作代码:

using LightGraphs
using GraphPlot
using Colors

g = graphfamous("karate")

membership = [1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1]
nodelabels = 1:34
nodecolor = [colorant"lightgrey", colorant"orange"]
nodefillc = nodecolor[membership]

colors = [colorant"lightgray" for i in  1:78]
colors[42] = colorant"orange"

gplot(g, nodefillc=nodefillc, layout=circular_layout, edgestrokec=colors, nodelabel=nodelabels)
Run Code Online (Sandbox Code Playgroud)

其产生: 在此输入图像描述

我成功获得了从 1 到 34 的节点标签,但是,我需要为某些特定节点显示另一种类型的标签。例如,某些节点的权重。也就是说,例如,我需要节点 19 的权重为 100,节点 1 的权重为 0.001。

有没有办法显示这样的数据?我可以在 GraphPlot 中找到相关关键字(只有 nodelabel 只接受向量),但我找不到另一个可以用于绘制图形的 Julia 包。

编辑感谢@Dan Getz,在发布到 SE 之前,我有与他建议相同的想法:尝试用格式的字符串标记节点"$i\n $weight" 但是,结果非常令人不满意,正如您在我的一张照片中看到的那样实际图表。橙色的节点 12,与其权重 177.0 分开,\n读起来不太好!

编辑感谢 @Przemyslaw Szufel 也许我的问题可以通过Compose(我实际上已经使用过)来解决,这是GraphPlot. …

label graph julia plots.jl

6
推荐指数
1
解决办法
203
查看次数

传递 Julia 参数以在 REPL 中执行?

我在日常工作中非常(非常!)经常这样做:

$ j7 # (alias for Julia version 1.7)
julia>
julia> ] activate .
(or similarly) (@v1.8) pkg> activate . 
Activating project at `~/MyMWE_WorkingPath`
julia> using Revise ; using RRSP # RRSP is my working module
Run Code Online (Sandbox Code Playgroud)

我想要一个别名,以便更快、更轻松地完成此例程。我每周至少这样做十次,并且会持续几个月。

理想情况下,我会做类似 j7RRSP 的操作,它是以下内容的别名:

alias j7RRSP='julia > ]activate . ; using Revise ; using RRSP'
Run Code Online (Sandbox Code Playgroud)

这是不正确的,我不知道如何做好!我知道执行 julia 文件的别名是可能的:

alias j7jl='julia somefiles.jl'
Run Code Online (Sandbox Code Playgroud)

就我而言,我正在激活一个项目,然后使用两个包:Revise(标准包),然后是我创建的 RRSP。

bash alias read-eval-print-loop julia

6
推荐指数
2
解决办法
368
查看次数

如何在 Julia 图中为彩色边缘添加图例?

以下是 Julia Discourse 的最小工作代码:

using LightGraphs
using GraphPlot
using Colors

g = graphfamous("karate")

membership = [1,1,1,1,1,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,2,1,2,1,1,1,1,2,1,1,2,1,1,1]
nodecolor = [colorant"lightgrey", colorant"orange"]
nodefillc = nodecolor[membership]

colors = [colorant"lightgray" for i in  1:78]
colors[42] = colorant"orange"

gplot(g, nodefillc=nodefillc, layout=circular_layout, edgestrokec=colors, edgelabel=1:78)
Run Code Online (Sandbox Code Playgroud)

哪个产生在此输入图像描述

我想在左上角(或右上角)添加一个图例,以便我可以轻松说出这两种边缘的含义。此外,如果还有一种方法来说明这两种类型的节点是什么,那就太好了!

例如,灰色边缘将被命名为:“常规边缘”,绿色边缘将被命名为“备用边缘”。同样,灰色节点将是“常规节点”,橙色节点将是“备份节点”!我在 gplot 中找不到我正在搜索的关键字!

colors legend julia gplots plots.jl

5
推荐指数
0
解决办法
151
查看次数

Julia - 如何知道最新版本(v1.0.0)的等效功能?

我之前ismatch在Julia v0.6.0中使用过函数,但是现在它返回了v1.0.0的错误,并且它不存在于v0.7.0或v1.0文档中.那么如何才能找到ismatch最新Julia版本的非折旧等价物呢?

更一般地说,在Julia中,如果它存在,如何知道任何折旧函数的等价物?

我的计算机上只安装了Julia v1.0.

julia

4
推荐指数
1
解决办法
948
查看次数

Julia - 在文件中打印或写入字符串中的反斜杠

我需要在 Julia 中写入一个文件,一个 LaTeX 命令,这是一个最小的例子:

open("./file.tex", "w") do f
   foo::Int = 5
   # write(f, "$foo\hline \n")        - This is what I would like to do
   # write(f, "$foo \\ \n")           - This is another thing I want to do, and it works
   # write(f, "$foo \\\hline \n")     - My final goal is this -> 
                                        leads to ERROR: syntax: invalid escape sequence
end
Run Code Online (Sandbox Code Playgroud)

你已经明白我想要什么了,我想要一个带有反斜杠的字符串,但不是作为转义字符。这不起作用:

julia> "\"  # Error
Run Code Online (Sandbox Code Playgroud)

既不是这个

julia> "\\\hline"
Run Code Online (Sandbox Code Playgroud)

string backslash julia

3
推荐指数
1
解决办法
1122
查看次数

Julia - 读取大文件的并行性

在 Julia v1.1 中,假设我有一个非常大的文本文件 (30GB) 并且我想要并行性(多线程)来读取每一行,我该怎么办?

这段代码是在检查Julia 的 multi-threading 文档后尝试这样做,但它根本不起作用

open("pathtofile", "r") do file
    # Count number of lines in file
    seekend(file)
    fileSize = position(file)
    seekstart(file)

    # skip nseekchars first characters of file
    seek(file, nseekchars)

    # progress bar, because it's a HUGE file
    p = Progress(fileSize, 1, "Reading file...", 40)
    Threads.@threads for ln in eachline(file)
        # do something on ln
        u, v = map(x->parse(UInt32, x), split(ln))
        .... # other interesting things
        update!(p, position(file))
    end    
end
Run Code Online (Sandbox Code Playgroud)

注意 …

parallel-processing multithreading julia

3
推荐指数
1
解决办法
537
查看次数

Julia - 将矩阵转换为向量

除了分配一个新向量并用我的矩阵一一填充它的值之外,我如何将大小矩阵的大小调整/重新填充n x m为大小向量,n x m概括以下示例:

\n
julia> example_matrix = [i+j for i in 1:3, j in 1:4]\n3\xc3\x974 Array{Int64,2}:\n 2  3  4  5\n 3  4  5  6\n 4  5  6  7\n\njulia> res_vect = [2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7]\n12-element Array{Int64,1}:\n 2\n 3\n 4\n 3\n 4\n 5\n 4\n 5\n 6\n 5\n 6\n 7\n
Run Code Online (Sandbox Code Playgroud)\n

我的一个想法是:

\n
 res_vect = Int[]\n for j in 1:size(example_matrix,2)\n    res_vect = vcat(res_vect, example_matrix[:,j])\n end\n
Run Code Online (Sandbox Code Playgroud)\n

我感觉这不是最佳方式,但我无法解释为什么......

\n

arrays performance vector matrix julia

3
推荐指数
1
解决办法
3116
查看次数

Julia - 具有Attribute的Mutable结构,它是一个Function和@code_warntype

在Julia中,我想要一个带有属性的可变结构,该类型是一个Function,这个函数将有参数:

mutable struct Class_example
    function_with_arguments::Function
    some_attribute::Int
    function Class_example() new() end
    function Class_example(function_wa::Function, some_a::Int)
        this = new()
        this.function_with_arguments = function_wa
        this.some_attribute = some_a
        this
    end
end
Run Code Online (Sandbox Code Playgroud)

我也想对这个可变结构做一个动作:

function do_action_on_class(Class::Class_example)
    return Class.function_with_arguments(Class.some_attribute ,2.0, true)
end
Run Code Online (Sandbox Code Playgroud)

然后我定义一个旨在成为我的类属性的函数:

function do_something_function(arg1::Int, arg2::Float64, arg3::Bool)
    if arg2 < 5.0
        for i in 1:arg1
            # Do Something Interesting
            @show arg3
        end
    end
    return 1
end
Run Code Online (Sandbox Code Playgroud)

最后,function_whith_arguments将在我的整个项目中启动大量时间,这只是一个最小的例子,所以我希望所有这些代码都非常快.这就是我根据Julia的文档性能提示使用@code_warntype的原因

但是,@ code_warntype告诉我这个

body::Any
15 1 ? %1 = (Base.getfield)(Class, :function_with_arguments)::Function
getproperty
%2 = (Base.getfield)(Class, :some_attribute)::Int64
%3 = (%1)(%2, …
Run Code Online (Sandbox Code Playgroud)

performance struct julia

2
推荐指数
1
解决办法
423
查看次数