下面是一个简单的代码示例:
Pkg.add("Graphs")
using Graphs
gd = simple_graph(20, is_directed=true) # directed graph with 20 nodes
nodeTo = 2
for nodeFrom in vertices(gd) # add some edges...
if(nodeTo != 20)
add_edge!(gd, nodeFrom, nodeTo)
nodeTo +=1
end
end
for i in edges(gd) # Pri
我的目标:对于一个项目,能够包含本地文件(相同项目的一部分,相同的目录),很普通,可能是循环的。
在Julia中,假设我有文件:
FILE1.jl
module A
include("FILE2.jl")
using B
# Type annotations that need types from B
end
FILE2.jl
module B
include("FILE1.jl")
using A
# Type annotations that need types from A
end
在Julia (>v1)中,这会导致无限循环。我已经
我有两个模块,一个称为mainLoop,另一个称为子程序:
module mainLoop
using subroutines
export memory
memory = zeros(Int,10)
foo(UInt32(17))
print(memory,"\n")
end
module subroutines
using mainLoop
export foo
function foo(x::UInt32)
mainLoop.memory[1]=x
end
end
我不知道如何从子例程模块中更改名为内存的数组的内容。我没有将数组作为参数传递给函数的选项,所以我必
我是Julia的新手,我知道我可以使用SpecialFunctions包来使用gamma函数,但是如果我这样写: using SpecialFunction
using ADCME
s = Variable(1.0); c = gamma(s) 然后我会遇到错误gamma not defined,那么我如何在PyObject中写伽马函数我的第二个问题是我可以写 using QuadGK
int = quadgk(f, a, b) 但是ADCME中没有quadgk函数,那我该怎么办呢?我应该用梯形法则或类似的方法来估计吗?
Julia结构和构造函数被组织在单独的文件中,如下所示:
# in file StructDef.jl
module StructDef
export MyStruct
struct MyStruct
ia::Int
end
end
# infile Constructors.jl
module Constructors
using StructDef
function MyStruct( )
# The following line throws the error: "ERROR: LoadError: MethodError: no method matc
我在这里写了一个模块:
# Hello.jl
module Hello
function foo
return 1
end
end
和
# Main.jl
using Hello
foo()
当我运行Main模块时:
$ julia ./Main.jl
我知道这个错误:
ERROR: LoadError: ArgumentError: Hello not found in path
in require at ./loading.jl:249
in include at ./boot.jl:261
in include_from_node1 at ./lo
我试图在julia中加载自定义模块,但总是会出现以下错误:
ERROR: foo not found
in require at loading.jl:47
其中foo是以下模块:
module foo
bar() = "foo"
export bar
end
它位于“/home/./julia/modules/”,我还补充说:
push!(LOAD_PATH, "/home/.../julia/modules/")
我的~/.juliarc.jl文件。我遗漏了什么?顺便说一下,我还重新安装了julia v0.3.10,没有效果。
还有一件事,如果我包括该文
在Julia中,如何在单独的模块中提供方法的默认实现?例如,如果抽象类型位于相同的模块中,我就没有问题,这就像我期望的那样工作: abstract type Foo end
howdy(x::Foo)::Union{String, Nothing} = nothing
struct Bar <: Foo end
function howdy(x::Bar)::Union{String, Nothing}
"I'm a Bar!"
end
struct Baz <: Foo end
if abspath(PROGRAM_FILE) == @
我想以类似于conda环境的方式创建Julia环境。但是,使用必要的包创建Project.toml意味着我无法使用本地/自定义模块。我有一个基本的项目结构 - julia_test
- MyModule.jl
- main.jl where MyModule.jl module MyModule
function f(a, b)
return (a+b)^2
end
end 和main.jl using Distributions
using MyModule
function main()
a = Normal()
b =
我正在尝试编写一个宏来调用带有关键字参数的函数(用于JuMP、NLexpressions和映射。这些函数只是用于访问数据库的函数。因此它们不表示数学运算)。
最小示例:
function foo1(; int::a=1)
a
end
function foo2(; int::a=1, int::b=2)
b
end
macro callfunc(f, keywordargs)
#function should be called here using symbol
#return values of f should be returned by macro
有没有办法在Julia中为可变结构添加默认参数? 我正在尝试编写类似以下内容的代码: mutable struct Scale
# Set default values that will be changed by fit!()
domain_min::Float64 = 0.0
domain_max::Float64 = 1.0
range_min::Float64 = 0.0
range_max::Float64 = 1.0
end
function fit!(data::Array)
# Set struct params he
在Julia中,可以导出函数(或变量、结构等)。在此之后,可以在另一个脚本中调用它,而不需要命名空间(一旦它被导入)。例如:
# helpers.jl
module helpers
function ordinary_function()
# bla bla bla
end
function exported_function()
# bla bla bla
end
export exported_function()
end
# main.jl
include("helpers.jl")
using .helpers
#we have to call the non-