julia

dealing with types in kwargs in Julia

拈花ヽ惹草 提交于 2020-04-10 05:04:07
问题 How can I use kwargs in a Julia function and declare their types for speed? function f(x::Float64; kwargs...) kwargs = Dict(kwargs) if haskey(kwargs, :c) c::Float64 = kwargs[:c] else c::Float64 = 1.0 end return x^2 + c end f(0.0, c=10.0) yields: ERROR: LoadError: syntax: multiple type declarations for "c" Of course I can define the function as f(x::Float64, c::Float64=1.0) to achieve the result, but I have MANY optional arguments with default values to pass, so I'd prefer to use kwargs.

User-defined infix operator

扶醉桌前 提交于 2020-04-09 17:56:28
问题 I know operators in Julia are just standard functions, and I can use them using the ordinary prefix call syntax: julia> +(1, 2) 3 However, they are also special in the sense that they can be (and are usually) used as infix operators: julia> 1+2 3 Could I define my own infix operator? If so, how? For example: julia> α(x, y) = x+y α (generic function with 1 method) julia> α(1, 2) 3 # as expected julia> 1α2 # expected result: 3 ERROR: UndefVarError: α2 not defined Stacktrace: [1] top-level scope

User-defined infix operator

人走茶凉 提交于 2020-04-09 17:53:01
问题 I know operators in Julia are just standard functions, and I can use them using the ordinary prefix call syntax: julia> +(1, 2) 3 However, they are also special in the sense that they can be (and are usually) used as infix operators: julia> 1+2 3 Could I define my own infix operator? If so, how? For example: julia> α(x, y) = x+y α (generic function with 1 method) julia> α(1, 2) 3 # as expected julia> 1α2 # expected result: 3 ERROR: UndefVarError: α2 not defined Stacktrace: [1] top-level scope

How do I do a correct micro-benchmark in Julia?

孤者浪人 提交于 2020-04-07 05:41:30
问题 The Julia 1.0.0 documentation provides general tips. It also suggests that instead of using the @time macro: For more serious benchmarking, consider the BenchmarkTools.jl package which among other things evaluates the function multiple times in order to reduce noise. How do they compare in use and is it worth the trouble to use something not in "base" Julia? 回答1: From a statistical point of view, @benchmark is much better than @time TL;DR The BenchmarkTools @benchmark macro is a great micro

How can a function have multiple return values in Julia (vs. MATLAB)?

孤街浪徒 提交于 2020-04-06 02:52:23
问题 In MATLAB, the following code returns m and s : function [m,s] = stat(x) n = length(x); m = sum(x)/n; s = sqrt(sum((x-m).^2/n)); end If I run the commands values = [12.7, 45.4, 98.9, 26.6, 53.1]; [ave,stdev] = stat(values) I get the following results: ave = 47.3400 stdev = 29.4124 How would I define my stat function in Julia? 回答1: How would I define my stat function in Julia? function stat(x) n = length(x) m = sum(x)/n s = sqrt(sum((x-m).^2/n)) return m, s end For more details, see the

Julia: create a new folder and file in location relative to script location

旧巷老猫 提交于 2020-03-25 03:56:38
问题 Would you happen to know how to get the path of a julia script, in a julia script? In essence, I built a julia script named someCode.jl and it is located in a folder named repository . When I run someCode.jl , I would like to create an output folder named output in the folder repository and put a file a.txt in folder output . With an absolute path it works fine, but I want it to be runnable for people who put it in a different path, but I have not been able to find the right syntax yet.

solve system of ODEs with read in external forcing

落爺英雄遲暮 提交于 2020-03-24 04:54:10
问题 In Julia, I want to solve a system of ODEs with external forcings g1(t), g2(t) like dx1(t) / dt = f1(x1, t) + g1(t) dx2(t) / dt = f2(x1, x2, t) + g2(t) with the forcings read in from a file. I am using this study to learn Julia and the package DifferentialEquations, but I am having difficulties finding the correct approach. I could imagine that using a callback could work, but that seems pretty cumbersome. Do you have an idea of how to implement such an external forcing? 回答1: You can use

solve system of ODEs with read in external forcing

大兔子大兔子 提交于 2020-03-24 04:53:54
问题 In Julia, I want to solve a system of ODEs with external forcings g1(t), g2(t) like dx1(t) / dt = f1(x1, t) + g1(t) dx2(t) / dt = f2(x1, x2, t) + g2(t) with the forcings read in from a file. I am using this study to learn Julia and the package DifferentialEquations, but I am having difficulties finding the correct approach. I could imagine that using a callback could work, but that seems pretty cumbersome. Do you have an idea of how to implement such an external forcing? 回答1: You can use

How do I load a UTF16-encoded text file in Julia?

徘徊边缘 提交于 2020-03-01 03:31:24
问题 I have a text file I am (pretty sure) is encoded in UTF16, but I don't know how to load it in Julia. Do I have to load it as bytes and then convert with UTF16String ? 回答1: The simplest way is to read it as bytes and then convert: s = open(filename, "r") do f utf16(readbytes(f)) end Note that utf16 also checks for a byte-order-mark (BOM), so it will deal with endianness issues and won't include the BOM in the resulting s . If you really want to avoid making a copy of the data, and you know it

分形的奥秘!分形着色器!shader 编程入门实战 ! Cocos Creator!

╄→гoц情女王★ 提交于 2020-02-27 17:05:07
> 极致的数学之美! 什么是分形? > “一个粗糙或零碎的几何形状,可以分成数个部分,且每一部分都(至少近似地)是整体缩小后的形状” 简单来说,分形 (fractal) 就像这个doge表情包一样,放大一部分后和原来的图近似。 用分形着色器实现的效果如下,在编辑器内放大其中的一部分,会发现与整体非常相似! 如何实现这么优雅的图片?一切起源于简单的公式 (julia set) 。 f(n) = f(n-1) * f(n-1) + c 通过迭代 n 次后可以实现分形效果。 起始值 f(0) 如何确定? 可以通过纹理坐标来确定。 当然这个起始值是个复数,有实数部分和虚数部分。我们用纹理 u 坐标表示实数, v 表示虚数部分。 纹理坐标的取值是 0-1 ,可以加一些偏移和缩放处理。 float real = (v_uv0.x-0.5)/zoom + offset.x; float image = (v_uv0.y-0.5)/zoom + offset.y; c 也是复数,对于不同的值,效果也不一样。 一次迭代如何计算?记得虚数部分 i*i = -1 就可以根据公式计算了,参考代码如下: float tmp_real = real; // 计算新的复数-实数部分 // f(n+1) = f(n)*f(n) + c // (a+bi)*(a+bi) + c = a*a - b*b + (2*a