# General Usage

**URL:** https://discourse.julialang.org/c/usage/6.md?page=724

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

**Page:** 725

---

## [Scope of variable](https://discourse.julialang.org/t/scope-of-variable/43396)

<div class="topic-metadata">

**Author:** [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Replies:** 7\
**Last updated:** [July 20, 2020, 7:59pm UTC](https://discourse.julialang.org/t/scope-of-variable/43396 "2020-07-20T19:59:16Z")

</div>

This code runs as expected when let block is uncommented: using AbstractPlotting, GLMakie using GLMakie.GLFW using GLMakie: to\_native # let npoints = 10 points = Node(Point3f0.(rand(npoints), rand(npoints), rand(n…

---

## [Add lines of code in a struct definition](https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297)

<div class="topic-metadata">

**Author:** [@Deduction42](https://discourse.julialang.org/u/Deduction42)\
**Replies:** 18\
**Last updated:** [July 20, 2020, 7:45pm UTC](https://discourse.julialang.org/t/add-lines-of-code-in-a-struct-definition/43297 "2020-07-20T19:45:29Z")

</div>

I have a way to generate a list of strings of lines of code I want to add in a struct. For example, let us say I generated a vector of strings: FieldDefinitions = \["A :: Float64", "B :: Int64"\] How would I go about add…

---

## [Copying struct fields to another struct](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493)

<div class="topic-metadata">

**Author:** [@aditya](https://discourse.julialang.org/u/aditya)\
**Replies:** 3\
**Last updated:** [July 20, 2020, 7:14pm UTC](https://discourse.julialang.org/t/copying-struct-fields-to-another-struct/22493 "2020-07-20T19:14:42Z")

</div>

For example I have an - abstract type A abstract type B \<: A struct C \<: B fields end Now, I want another struct D which has all fields of C but is a subtype of A i.e, struct D \<: A C fields end Is there a way…

---

## [How to make plot legend have names instead of "y1, y2, ..., yn"](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270)

<div class="topic-metadata">

**Author:** [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Replies:** 7\
**Last updated:** [July 20, 2020, 5:27pm UTC](https://discourse.julialang.org/t/how-to-make-plot-legend-have-names-instead-of-y1-y2-yn/43270 "2020-07-20T17:27:31Z")

</div>

Much to my chagrin, after reading the Plots.jl Tutorial in its entirety I can’t get this to work: histogram(\[\[1,2,3,1\],\[1,2,2,2,2\]\],labels=\["foo","bar"\]) Gives (obviously I don’t want arrays for variable names): May…

---

## [How to test if something is a stack trace](https://discourse.julialang.org/t/how-to-test-if-something-is-a-stack-trace/43363)

<div class="topic-metadata">

**Author:** [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Replies:** 2\
**Last updated:** [July 20, 2020, 10:08am UTC](https://discourse.julialang.org/t/how-to-test-if-something-is-a-stack-trace/43363 "2020-07-20T10:08:01Z")

</div>

I have a function that works on an array and collects the exceptions and backtraces encountered during processing. For the tests I need to check if something is a backtrace. The following works, but seems very fragile: …

---

## [Question about Clustering](https://discourse.julialang.org/t/question-about-clustering/43359)

<div class="topic-metadata">

**Author:** [@abcde](https://discourse.julialang.org/u/abcde)\
**Replies:** 7\
**Last updated:** [July 20, 2020, 7:28am UTC](https://discourse.julialang.org/t/question-about-clustering/43359 "2020-07-20T07:28:51Z")

</div>

Could you please introduce some Pkg based on TNN clustering?thankS！！！

---

## [Reducing dynamic dispatch using type inference?](https://discourse.julialang.org/t/reducing-dynamic-dispatch-using-type-inference/43233)

<div class="topic-metadata">

**Author:** [@gobs](https://discourse.julialang.org/u/gobs)\
**Replies:** 4\
**Last updated:** [July 20, 2020, 7:17am UTC](https://discourse.julialang.org/t/reducing-dynamic-dispatch-using-type-inference/43233 "2020-07-20T07:17:37Z")

</div>

First of all, apologies for the terrible title, but I’m having a hard time describing what I want to do. I’ve written a lot of my code for an optimisation problem to look something like this: using JuMP # Function def…

---

## [\[ForwardDiff Question\]: Jacobian of a function that calls another function](https://discourse.julialang.org/t/forwarddiff-question-jacobian-of-a-function-that-calls-another-function/43276)

<div class="topic-metadata">

**Author:** [@Jaepil\_Lee](https://discourse.julialang.org/u/Jaepil_Lee)\
**Replies:** 6\
**Last updated:** [July 20, 2020, 1:41am UTC](https://discourse.julialang.org/t/forwarddiff-question-jacobian-of-a-function-that-calls-another-function/43276 "2020-07-20T01:41:29Z")

</div>

I don’t understand why my vector-valued function does not produce Jacobian using ForwardDiff.jl. Could you help me how can I make it work? Ultimately I am gonna call that resulting Jacobian P and try to compute P’P\\P(som…

---

## [Kramer-type SDE - conditionally stop solution process](https://discourse.julialang.org/t/kramer-type-sde-conditionally-stop-solution-process/37866)

<div class="topic-metadata">

**Author:** [@StarecZosima](https://discourse.julialang.org/u/StarecZosima)\
**Replies:** 3\
**Last updated:** [July 19, 2020, 7:08pm UTC](https://discourse.julialang.org/t/kramer-type-sde-conditionally-stop-solution-process/37866 "2020-07-19T19:08:10Z")

</div>

Hi there, I am trying to solve a Kramer-type SDE, simulating a particle in a double well. I use the DifferentialEquations package and it all works well until the particle reaches the top of the barrier and start rollin…

---

## [Error using Sundials and JuMP](https://discourse.julialang.org/t/error-using-sundials-and-jump/42272)

<div class="topic-metadata">

**Author:** [@Jorge\_Silva](https://discourse.julialang.org/u/Jorge_Silva)\
**Replies:** 1\
**Last updated:** [June 29, 2020, 11:33pm UTC](https://discourse.julialang.org/t/error-using-sundials-and-jump/42272 "2020-06-29T23:33:37Z")

</div>

First I ve defined a function that uses Sundials solver. function rheo(czz0) … prob = DAEProblem(filament,du₀,u₀,tspan,differential\_vars=differential\_vars) sol = Sundials.solve(prob, IDA(), reltol=1e-8, abstol=1e-8) …

---

## [Distributed, packages, and race condition in pre-compiling](https://discourse.julialang.org/t/distributed-packages-and-race-condition-in-pre-compiling/43340)

<div class="topic-metadata">

**Author:** [@torgo](https://discourse.julialang.org/u/torgo)\
**Replies:** 1\
**Last updated:** [July 19, 2020, 5:35pm UTC](https://discourse.julialang.org/t/distributed-packages-and-race-condition-in-pre-compiling/43340 "2020-07-19T17:35:51Z")

</div>

I am having a problem that I think is represented well by this StackExchange question discussion. As far as I understand that discussion, one needs to be careful when using Distributed to balance two issues: Each work…

---

## [Linear least-squares solution occasionally much slower than what is achievable using LAPACK based wrappers](https://discourse.julialang.org/t/linear-least-squares-solution-occasionally-much-slower-than-what-is-achievable-using-lapack-based-wrappers/43337)

<div class="topic-metadata">

**Author:** [@andreasvarga](https://discourse.julialang.org/u/andreasvarga)\
**Replies:** 0\
**Last updated:** [July 19, 2020, 3:46pm UTC](https://discourse.julialang.org/t/linear-least-squares-solution-occasionally-much-slower-than-what-is-achievable-using-lapack-based-wrappers/43337 "2020-07-19T15:46:00Z")

</div>

The computation of the least-squares solution of linear equations using x = A\\b is occasionally substantially slower than the attainable speed using LAPACK based wrappers. For example, let A be a full matrix A = rand(1…

---

## [Base.getproperty via Val types](https://discourse.julialang.org/t/base-getproperty-via-val-types/43195)

<div class="topic-metadata">

**Author:** [@jackf](https://discourse.julialang.org/u/jackf)\
**Replies:** 7\
**Last updated:** [July 19, 2020, 5:09am UTC](https://discourse.julialang.org/t/base-getproperty-via-val-types/43195 "2020-07-19T05:09:28Z")

</div>

I’m familiar with Base.getproperty, and often structure it as a big list of if statements, with a fallback to getfield. Sometimes, though, having to define all the behaviour in one place can be a bit annoying. I thought…

---

## [How to manually update the params in a \`Flux.Chain\` neural network?](https://discourse.julialang.org/t/how-to-manually-update-the-params-in-a-flux-chain-neural-network/43267)

<div class="topic-metadata">

**Author:** [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Replies:** 1\
**Last updated:** [July 19, 2020, 5:14am UTC](https://discourse.julialang.org/t/how-to-manually-update-the-params-in-a-flux-chain-neural-network/43267 "2020-07-19T05:14:03Z")

</div>

I have a simple Flux neural network, e.g. nn = Chain( Dense(20, 20), Dense(20, 2) ) I wish to compute the gradient of the log(nn(input)) wrt to Flux.Params(nn). I can’t use the Flux.train! and use a loss function…

---

## [Confusion regarding Threads.@spawn performance](https://discourse.julialang.org/t/confusion-regarding-threads-spawn-performance/43210)

<div class="topic-metadata">

**Author:** [@Samuel\_Ainsworth](https://discourse.julialang.org/u/Samuel_Ainsworth)\
**Replies:** 4\
**Last updated:** [July 19, 2020, 4:43am UTC](https://discourse.julialang.org/t/confusion-regarding-threads-spawn-performance/43210 "2020-07-19T04:43:27Z")

</div>

I’m not able to use Threads.@spawn with the kind of performance I’d expect: samuela@n64:~/dev/research/julia/odecontrol$ JULIA\_NUM\_THREADS=32 julia --project \_ \_ \_ \_(\_)\_ | Documentation: htt…

---

## ['Reducing' scope of global variables in functions](https://discourse.julialang.org/t/reducing-scope-of-global-variables-in-functions/43256)

<div class="topic-metadata">

**Author:** [@1ozturkbe](https://discourse.julialang.org/u/1ozturkbe)\
**Replies:** 9\
**Last updated:** [July 19, 2020, 2:34am UTC](https://discourse.julialang.org/t/reducing-scope-of-global-variables-in-functions/43256 "2020-07-19T02:34:04Z")

</div>

I have a question with respect to globals in functions. I rely a lot on generating functions inside functions that take in data arguments. However, I am finding that changing the data (eg. a and idxs in the global scope …

---

## [Performance of findmax vs. raw loop](https://discourse.julialang.org/t/performance-of-findmax-vs-raw-loop/43305)

<div class="topic-metadata">

**Author:** [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Replies:** 12\
**Last updated:** [July 19, 2020, 1:23am UTC](https://discourse.julialang.org/t/performance-of-findmax-vs-raw-loop/43305 "2020-07-19T01:23:24Z")

</div>

I’m curious if there is a high level concept to find maximum element and index in mapped array, which has similar performance to raw loop. I’m comparing f4 and f5: using BenchmarkTools, Random, StaticArrays, LinearAlgeb…

---

## [Summation of rows of array without allocation](https://discourse.julialang.org/t/summation-of-rows-of-array-without-allocation/43313)

<div class="topic-metadata">

**Author:** [@aaraujo71](https://discourse.julialang.org/u/aaraujo71)\
**Replies:** 1\
**Last updated:** [July 18, 2020, 11:42pm UTC](https://discourse.julialang.org/t/summation-of-rows-of-array-without-allocation/43313 "2020-07-18T23:42:14Z")

</div>

Suppose we have an array x with m rows and n columns and a vector s with m elements. I would like to sum the elements of each row of array x and save these results in vector s. This can be easily implemented in Julia us…

---

## [Round inplace?](https://discourse.julialang.org/t/round-inplace/43298)

<div class="topic-metadata">

**Author:** [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Replies:** 9\
**Last updated:** [July 18, 2020, 9:07pm UTC](https://discourse.julialang.org/t/round-inplace/43298 "2020-07-18T21:07:39Z")

</div>

What is the reason that there are not round! functions? Is it not possible to do that type of operation in place? It seems like it is no different than replace! (which exists) and the thing you are replacing with is simp…

---

## [Unintuitive behaviour with BenchmarkTools](https://discourse.julialang.org/t/unintuitive-behaviour-with-benchmarktools/43291)

<div class="topic-metadata">

**Author:** [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Replies:** 12\
**Last updated:** [July 18, 2020, 5:39pm UTC](https://discourse.julialang.org/t/unintuitive-behaviour-with-benchmarktools/43291 "2020-07-18T17:39:03Z")

</div>

I’m baffled by this error. Here is the relevant code fragment: function f1(p) total = 0. for i in 1:length(p)-2 q = p\[i\] a = p\[i+1\] b = p\[i+2\] dx = b.x - a.x dy = b.y - …

---

## [What does sizeof return for wrappers of arrays?](https://discourse.julialang.org/t/what-does-sizeof-return-for-wrappers-of-arrays/43237)

<div class="topic-metadata">

**Author:** [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Replies:** 1\
**Last updated:** [July 17, 2020, 3:21pm UTC](https://discourse.julialang.org/t/what-does-sizeof-return-for-wrappers-of-arrays/43237 "2020-07-17T15:21:39Z")

</div>

Eg: julia\> a = ones(10); julia\> sizeof(a) 80 julia\> sizeof(a') 8 julia\> a = ones(ComplexF64, 10); julia\> sizeof(a') 8 I’m not sure how to interpret the results for the adjoint? Similar issues arise for OffsetArrays…

---

## [Built-in vs. user-defined functions](https://discourse.julialang.org/t/built-in-vs-user-defined-functions/43213)

<div class="topic-metadata">

**Author:** [@andreas.karatzas](https://discourse.julialang.org/u/andreas.karatzas)\
**Replies:** 13\
**Last updated:** [July 18, 2020, 2:07pm UTC](https://discourse.julialang.org/t/built-in-vs-user-defined-functions/43213 "2020-07-18T14:07:50Z")

</div>

Hello, I am relatively new to Julia. I am mainly interested in Julia because of the performance. There are some functions that are built in Julia like sum(). A custom sum function is worse in performance than the built-…

---

## [Subvector of SVector](https://discourse.julialang.org/t/subvector-of-svector/43273)

<div class="topic-metadata">

**Author:** [@pauljurczak](https://discourse.julialang.org/u/pauljurczak)\
**Replies:** 2\
**Last updated:** [July 18, 2020, 12:42pm UTC](https://discourse.julialang.org/t/subvector-of-svector/43273 "2020-07-18T12:42:46Z")

</div>

In this code snippet: using StaticArrays Vec2f = SVector{2, Float32} Vec3f = SVector{3, Float32} a = Vec3f(1, 2, 3) b = a\[1:2\] c = a\[\[true, false, true\]\] b and c type is Array{Float32,1}. How do I ma…

---

## [Surprising StackOverflowError](https://discourse.julialang.org/t/surprising-stackoverflowerror/43277)

<div class="topic-metadata">

**Author:** [@Bigorneau](https://discourse.julialang.org/u/Bigorneau)\
**Replies:** 2\
**Last updated:** [July 18, 2020, 12:40pm UTC](https://discourse.julialang.org/t/surprising-stackoverflowerror/43277 "2020-07-18T12:40:44Z")

</div>

I encountered a surprising error today. The following two lines result in a StackOverflowError (with Julia 1.4.2): f(x::T, y::T, z::S) where {S,T\<:Vector{S}} = "method 1" f(x::T, z::S, y::T) where {S,T\<:Vector{S}} = "me…

---

## [JSON write and read back Arrays](https://discourse.julialang.org/t/json-write-and-read-back-arrays/43283)

<div class="topic-metadata">

**Author:** [@sz1992](https://discourse.julialang.org/u/sz1992)\
**Replies:** 2\
**Last updated:** [July 18, 2020, 12:38pm UTC](https://discourse.julialang.org/t/json-write-and-read-back-arrays/43283 "2020-07-18T12:38:21Z")

</div>

I would like to write some multi-D Array into JSON and parse it back. I tried using JSON.jl : julia\> using JSON julia\> ar=rand(2,2,2) 2×2×2 Array{Float64,3}: \[:, :, 1\] = 0.238296 0.813746 0.156503 0.953343 \[:,…

---

## [Is there a way to set Non Negativity, with CVODE\_BDF()~ Sundials Pkg, as isoutofdomain() was ignored-Thanks?](https://discourse.julialang.org/t/is-there-a-way-to-set-non-negativity-with-cvode-bdf-sundials-pkg-as-isoutofdomain-was-ignored-thanks/42960)

<div class="topic-metadata">

**Author:** [@cbasile21](https://discourse.julialang.org/u/cbasile21)\
**Replies:** 7\
**Last updated:** [July 18, 2020, 2:27am UTC](https://discourse.julialang.org/t/is-there-a-way-to-set-non-negativity-with-cvode-bdf-sundials-pkg-as-isoutofdomain-was-ignored-thanks/42960 "2020-07-18T02:27:09Z")

</div>

Hello there, Earlier I had asked a question about prevent Domain errors , and non-negativity, Related to this, I want to apply this to CVODE\_BDF, but I believe is not compatible with Sundials, it raises the error it …

---

## [Performance issue when searching for an occurrence in larger matrix](https://discourse.julialang.org/t/performance-issue-when-searching-for-an-occurrence-in-larger-matrix/43209)

<div class="topic-metadata">

**Author:** [@a.fas](https://discourse.julialang.org/u/a.fas)\
**Replies:** 3\
**Last updated:** [July 18, 2020, 1:11am UTC](https://discourse.julialang.org/t/performance-issue-when-searching-for-an-occurrence-in-larger-matrix/43209 "2020-07-18T01:11:39Z")

</div>

I wrote some code that searches a large matrix to find occurrences of a given vector. MWE: A = repeat(\[1 2 3; 4 5 6; 7 8 9\], 100, 1); b = \[4; 5; 6\]; In this case, what I would want is a boolean vector that is true for …

---

## [Worse Performance with Approximate Minimum Degree Reordering of Sparse Matrices + LU Than \`\\\`?](https://discourse.julialang.org/t/worse-performance-with-approximate-minimum-degree-reordering-of-sparse-matrices-lu-than/43212)

<div class="topic-metadata">

**Author:** [@jchen975](https://discourse.julialang.org/u/jchen975)\
**Replies:** 4\
**Last updated:** [July 18, 2020, 12:17am UTC](https://discourse.julialang.org/t/worse-performance-with-approximate-minimum-degree-reordering-of-sparse-matrices-lu-than/43212 "2020-07-18T00:17:43Z")

</div>

I’m trying to test the supposed boost in performance (as seen from Matlab) when AMD reordering is used before factorization with the lu when solving Ax = b, where A is a real valued n \\times n sparse matrix. Here is my…

---

## [Best generic method to solve homogeneous system](https://discourse.julialang.org/t/best-generic-method-to-solve-homogeneous-system/43261)

<div class="topic-metadata">

**Author:** [@jebej](https://discourse.julialang.org/u/jebej)\
**Replies:** 3\
**Last updated:** [July 17, 2020, 10:35pm UTC](https://discourse.julialang.org/t/best-generic-method-to-solve-homogeneous-system/43261 "2020-07-17T22:35:51Z")

</div>

What is the best way to solve the homogeneous system A\\vec{x}=\\vec{0} for either a dense or sparse array (or even GPU array)? For dense A, nullspace(A) works, and for a sparse array, eigs(A,nev=1,which=:SM) appears to w…

---

## [Reduce vs. foldl: performance and precision](https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231)

<div class="topic-metadata">

**Author:** [@Boo](https://discourse.julialang.org/u/Boo)\
**Replies:** 11\
**Last updated:** [July 17, 2020, 9:44pm UTC](https://discourse.julialang.org/t/reduce-vs-foldl-performance-and-precision/43231 "2020-07-17T21:44:35Z")

</div>

Hi guys, Can you explain me, please, the difference in performance and precision between reduce and foldl? julia\> using BenchmarkTools julia\> a = rand(10^7) 10000000-element Array{Float64,1}: 0.8488959437260439 0.76…

[Previous page](https://discourse.julialang.org/c/usage/6.md?page=723)

[Next page](https://discourse.julialang.org/c/usage/6.md?page=725)
