Quantcast
Channel: First steps - JuliaLang
Viewing all 2795 articles
Browse latest View live

Extract exponent from numbers in julia

$
0
0

@Luigi_Marongiu wrote:

Hello,
I have to compare numbers that are quite small – something like 10e-5 or 10e-13 etc. Is there a way to extract the exponent (or characteristic) of the numbers, so that I will be working with -5 and -13 only? (thus passing from a float64 to a more friendly number)
Is it possible to extract such characteristic from a normal number? or instance, 2 should give 0 (as in 2x10e0).
I would I convert a number into scientific? thus 2 into 2e0?
Thank you

Posts: 2

Participants: 2

Read full topic


MethodError: no method matching -(::NTuple{4,Int64}, ::Int64)

$
0
0

@marinarrd wrote:

I am new and dont understand why this doesnt work!

original_cost = npzread("file.npy")
size_cost = size(original_cost)
println('size of original table',size_cost)
left_index = 40
right_index = size_cost - left_index
println(left_index, right_index)
cost = copy(original_cost)
cost=cost[left_index:right_index,left_index:right_index,left_index:right_index,left_index:right_index]

with output

size of original table(101, 101, 101, 101)
MethodError: no method matching -(::NTuple{4,Int64}, ::Int64)
Closest candidates are:
  -(!Matched::Complex{Bool}, ::Real) at complex.jl:299
  -(!Matched::Missing, ::Number) at missing.jl:94
  -(!Matched::Base.CoreLogging.LogLevel, ::Integer) at logging.jl:107
  ...

Stacktrace:
 [1] top-level scope at In[9]:5

Posts: 4

Participants: 2

Read full topic

Tuple of Arrays: assigning or accumulating into the first column of each tuple elements

$
0
0

@jlchan wrote:

I’m trying to apply a matrix A to a single column of each element of X and accumulate into X, where X is a tuple of arrays.

N = 2
A = rand(N,N)
X = (ones(N,N),ones(N,N))
for fld in eachindex(X)
    X[fld][:,1] += A*X[fld][:,1]
end    

I can apply A*X[fld][:,1] to all fields using map and broadcasted getindex, but I get an error if I try to set or accumulate into the columns of elements of the tuple

getindex.(X,(:),1) .+= map(x->A*x,getindex.(X,(:),1)) # doesn't work

Is there a way to do this without looping over the elements of the struct?

Posts: 5

Participants: 2

Read full topic

Storage of a diagonal matrix (and best way to update)?

$
0
0

@purplishrock wrote:

I have a typical situation,

P is an m x n matrix
Q is an m x m matrix, but only has diagonal elements, i.e.

Q[i,j] = q[k], i=j=k
Q[i,j] = 0, i != j

and eventually I have to multiply Q and P,

R = Q * P

Naturally, m in my situation is a fairly large number, ~1000, which, if Diagonal is actually stored as a full Matrix is a million elements. I really expect that it is NOT stored that way but is stored as a Diagonal type which is associated with a vector of 1000 elements, i.e. it uses 1000 memory locations.

The values of Q are going to be updated via iteration, therefore allocate once and update is the way to go. There are two ways to do this,

Create the diagonal matrix,

Q = Diagonal(q)

and then update directly,

for i=1:m 
  Q[i,i] = update(i)
end
R = Q * P

or update the base vector and create the Diagonal again before multiplying,

for i=1:m
  q[i] = update(i)
end

R = Diagonal(q) * P

which of course seems like a bad idea since i’m creating the Diagonal instance on every iteration…

I’m really expecting the right answer to be “directly update the Diagonal matrix Q”, just wanted to be sure.

Posts: 4

Participants: 3

Read full topic

LightGraphs

$
0
0

@ennvvy wrote:

I am trying to generate a regular-directed graph and eventually a small-world network using watts_strogatz function in the Light Graphs’ package. For a network with 10,000 nodes, the time taken is as below:

@btime watts_strogatz(10000,2500,0.0,is_directed=true)
  1.841 s (220005 allocations: 634.00 MiB)
{10000, 12500000} directed simple Int64 graph

Is there anyway that I could make this faster?

Posts: 7

Participants: 5

Read full topic

How to Parse Date or Time in multiple possible format?

$
0
0

@Eric_Chen wrote:

My user upload a CSV file with “Time” column
Sometimes could be HHMMSS, sometimes could be HH:MM:SS, sometimes could be other format, like HHMM.

How can I read the CSV file and automatically convert the “Time” column to a Time object?

Can I parse it with something like: " HHMMSS|HH:MM:SS " --> this is doable in C#.

Posts: 6

Participants: 3

Read full topic

GPU support in Knet: Cannot allocate before CuArrays has been initialized

$
0
0

@Peter.Zentis wrote:

Hi all,
I am trying to work through the Knet tutorial (https://github.com/denizyuret/Knet.jl/blob/master/tutorial/50.cnn.ipynb) which worked great, just until I needed/wanted to use gpu support i.e. use a KnetArray. I suppose the problem is related to my CUDA setup. The output of nvidia-smi looks good to me and I do not know what else to look at:

nvidia-smi
Fri Nov  8 13:39:57 2019       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.87.00    Driver Version: 418.87.00    CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:03:00.0  On |                  N/A |
|  0%   43C    P8    14W / 250W |    233MiB / 11169MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      2606      G   /usr/bin/X                                    38MiB |
|    0      2805      G   /usr/bin/gnome-shell                          47MiB |
|    0     14149      C   /opt/julia-1.2.0/bin/julia                   135MiB |
+-----------------------------------------------------------------------------+

Also I successfully tested nvcc as described here: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#compiling-examples

I am not sure whether this is relevant for this issue, but I am on a Opensuse Leap 15.0 system, and tried to install CUDA according to the official instructions.

Anyway, this line from the Knet tutorial fails with an error:

x = KnetArray(reshape([1.0:6.0...], (6,1,1,1)))

And I get the following error:

ERROR: AssertionError: Cannot allocate before CuArrays has been initialized.
Stacktrace:
 [1] CuArrays.CuArray{UInt8,1,P} where P(::UndefInitializer, ::Tuple{Int64}) at /root/.julia/packages/CuArrays/BcKUl/src/memory.jl:120
 [2] Type at /root/.julia/packages/CuArrays/BcKUl/src/array.jl:98 [inlined]
 [3] Type at /root/.julia/packages/CuArrays/BcKUl/src/array.jl:99 [inlined]
 [4] KnetPtrCu(::Int64) at /root/.julia/packages/Knet/HRYiN/src/cuarray.jl:90
 [5] Type at /root/.julia/packages/Knet/HRYiN/src/kptr.jl:102 [inlined]
 [6] Type at /root/.julia/packages/Knet/HRYiN/src/karray.jl:95 [inlined]
 [7] KnetArray(::Array{Float64,4}) at /root/.julia/packages/Knet/HRYiN/src/karray.jl:93
 [8] top-level scope at REPL[4]:1

Apparently, the problem is due to my CUDA setup?! At least I get a bunch of errors on pkg> test CuArrays:

Test Summary:                                   | Pass  Fail  Error  Total
CuArrays                                        |   63     1   1124   1188
  GPUArrays test suite                          |   25          431    456
    construction                                |   15            7     22
      constructors + similar                    |                 1      1
      comparison against Array                  |   15            1     16
      conversion                                |                 1      1
      value constructors                        |                 1      1
      iterator constructors                     |                 3      3

  ...

    Batchnorm                                   |                 1      1
  CUTENSOR                                      |                    No tests
  ForwardDiff                                   |   10            9     19
    UNARY                                       |                    No tests
    POW                                         |                 8      8
    LITERAL_POW                                 |   10                  10
    Broadcast Fix                               |                 1      1
ERROR: LoadError: Some tests did not pass: 63 passed, 1 failed, 1124 errored, 0 broken.
in expression starting at /home/kzentis/.julia/packages/CuArrays/BcKUl/test/runtests.jl:23
ERROR: Package CuArrays errored during testing

This is just the end of the output, I can post it all if required.
It would be really great, if someone could point me where to search for the underlying cause of my problem.

Thanks, Peter

Posts: 3

Participants: 2

Read full topic

Assigning a Tuple to sub-entries of Tuple of Arrays

$
0
0

@jlchan wrote:

I’m distributing entries of a Tuple to the ith entry of each array in a Tuple of Arrays.

using BenchmarkTools

N = 3
X = (zeros(N),zeros(N))
Y = [1,2]
function foo1!(X,Y,i)
    for fld in eachindex(X)
        X[fld][i] = Y[fld]
    end
end
@btime foo1!($X,$Y,$1)

foo2!(x,y,i) = x[i] = y
@btime foo2!.($X,$Y,$1)

foo3!(X,Y,i) = getindex(X,i) = Y
@btime foo3!($X,$Y,$1)

Each different approach gives a different runtimes

  2.608 ns (0 allocations: 0 bytes)
  44.127 ns (1 allocation: 96 bytes)
  5.209 ns (1 allocation: 16 bytes)

Why is the second so slow, and is there a one-line approach which is as fast as the for loop?

Posts: 3

Participants: 2

Read full topic


How to find k in introductory population growth problem?

What's the `Pkg` equivalent of `pin Pkg1@0.5.0`?

$
0
0

@xiaodai wrote:

I can do Pkg.add("Pkg1") which is the same as ]add Pkg1.

What’s the equivalent of ]pin Pkg1@0.5.0? I can’t run this in Jupyter notebook.

I tried Pkg.pin("Pkg1", v"0.50") but it doesn’t have this signature.

Posts: 2

Participants: 2

Read full topic

First steps with Jupyterlab

$
0
0

@Usor wrote:

If this is not in the correct forum, can someone move ot to the right one? Maybe Tooling? Thanks.

I don’t know much about Jupyterlab, so I am here to learn. I read up on it, but I need some to-the-point guidance and answers. I saw it in action and was impressed, as it looks better to me than VS Code and certainly better than a poor terminal. But once I learned it’s always online, I was turned off. So here are some basic questions. I hope you will properly educate me.

What is Jupyterlab?

What are its essential functions?

Can it be used fully offline (like an offline IDE)?

If online, it makes use of some online server, including extra computing power, to process things?

Does it have (some) Vim ways?

Posts: 4

Participants: 2

Read full topic

Dynamic nested For-Loops? (dumb question)

$
0
0

@ckneale wrote:

This is going to sound stupid - but I have a weird question. So I’m working on a dynamic programming problem just for fun, nothing novel (been solved 20yrs ago). I’ve always found the dichotomy between recursion and loops very interesting. Sometimes I think we’re missing an operator in Computer Science… Anyways,

In Julia is it possible to dynamically dispatch a series of for-next loops? I can solve the problem I want too pretty straightforwardly with recursion but my experiences with Julia and recursion have been that there’s some performance cost(usually nothing major). I imagine this could be done via a while loop right.

But imagine we needed to track information as we’re going through the loops and things, preserve order of which loop to go to next for performance, etc. The control structure is more linear than maybe recursion but, seems clunky.

Is there a more elegant way to tie this down? Is metaprogramming too far? FoldLeft over for loops? or something crazy like that.

Edit - I also do realize iterators make life a lot easier for these tasks, I’m just trying to get at the fundamentals.

Posts: 1

Participants: 1

Read full topic

Generalized Schur Decomposition with sparse matrices?

$
0
0

@qwerytyui1234 wrote:

Hey guys,

so I’m trying to use generalized schur decomposition, but with large sparse matrices.

I know I can use schurfact(A,B) in the LinearAlgebra package, but it seems that it does not allow for sparse matrices.

I found qr function that does QR decomposition in SuiteSparse.jl, but I was wondering if anybody knew of QZ decompositions?

I am completely fine by using the one in the LinearAlgebra package, but I was just curious!

Thanks!

Posts: 1

Participants: 1

Read full topic

What is the unicode for the number i, the imaginary unit of the complex numbers

$
0
0

@StevenSiew wrote:

Er? How do I type in the imaginary unit in Julia code as an unicode character? Also how do I type it in program Textedit in normal Mac text document?

The number i, the imaginary unit of the complex numbers.

PS: I want to type it in as a single character not “im”

Posts: 4

Participants: 3

Read full topic

Resolved: Arpack deps.jl Linux build issue

$
0
0

@pontus wrote:

Just putting this here in event anyone else has similar issues with a linux Julia install from source on an Arch Linux kernel (this may not be an issue if installing from binaries atm). This requires root access, so may not work for those on university clusters, etc. (if you search, there are some ways to use your home directory to resolve this as well).
Distro: Manjaro Linux 18.1.2
Kernel: 5.3.8-3-MANJARO
Julia: v1.2, installed using ‘pacman’

This would have occurred with Distributions and others depending on Arpack, not just StatsPlots, just where it manifested first in my case:

julia> using StatsPlots
[ Info: Precompiling StatsPlots [f3b207a7-027a-5e70-b257-86293d7955fd]
ERROR: LoadError: No deps.jl file could be found. Please try running Pkg.build("Arpack").
Currently, the build command might fail when Julia has been built from source
and the recommendation is to use the official binaries from julialang.org.
For more info see https://github.com/JuliaLinearAlgebra/Arpack.jl/issues/5.

Resolved by creating a symlink:

$ sudo ln -s /usr/lib/libopenblas.so /usr/lib/libopenblas64_.so.0

More details to understand if this might apply to your situation and offer resolution:
First, had installed using pacman, not from Julia source binaries:

[e@xps15 ~]$ sudo pacman -Sy julia
[sudo] password for e:
:: Synchronizing package databases...
 core is up to date
 extra is up to date
 community is up to date
 multilib is up to date
resolving dependencies...
looking for conflicting packages...
:: openblas and blas are in conflict. Remove blas? [y/N] y

Packages (6) blas-3.8.0-2 [removal]  cblas-3.8.0-2  http-parser-2.9.2-1  libgit2-1:0.28.3-1  openblas-0.3.7-1  julia-2:1.2.0-1.2

Total Download Size:    31.87 MiB
Total Installed Size:  203.86 MiB
Net Upgrade Size:      203.53 MiB

:: Proceed with installation? [Y/n] Y
:: Retrieving packages...
 cblas-3.8.0-2-x86_64                                                              30.1 KiB  9.78M/s 00:00 
...
(5/5) installing julia                                                                                     [################################################################] 100%
Optional dependencies for julia
    gnuplot: If using the Gaston Package from julia
:: Running post-transaction hooks...
(1/3) Updating icon theme caches...
(2/3) Arming ConditionNeedsUpdate...
(3/3) Updating the desktop file MIME type cache...

[e@xps15 ~]$ julia
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.2.0 (2019-08-20)
 _/ |\__'_|_|_|\__'_|  |
|__/                   |

julia>

All seemed fine, and was using Julia v1.2 for several days with other packages that didn’t require Arpack, so didn’t notice the issue at first.
Arpack seemed to work fine with adding the package, but failed here:

(v1.2) pkg> build Arpack
  Building Arpack → `~/.julia/packages/Arpack/cu5By/deps/build.log`
┌ Error: Error building `Arpack`: 
│ ERROR: LoadError: LibraryProduct(nothing, ["libarpack"], :libarpack, "Prefix(/home/e/.julia/packages/Arpack/cu5By/deps/usr)") is not satisfied, cannot generate deps.jl!
...

Couldn’t remove it using rm as apparently it wasn’t actually added:

(v1.2) pkg> rm Arpack
ERROR: The following package names could not be resolved:
 * Arpack (7d9fca2a-8960-54d3-9f78-7d1dccf2cb97 in manifest but not in project)
Please specify by known `name=uuid`.

(v1.2) pkg> rm 7d9fca2a-8960-54d3-9f78-7d1dccf2cb97
┌ Warning: `7d9fca2a-8960-54d3-9f78-7d1dccf2cb97` not in project, ignoring
└ @ Pkg.Operations /build/julia/src/julia-1.2.0/usr/share/julia/stdlib/v1.2/Pkg/src/Operations.jl:873
[ Info: No changes

Tried to update and then build, no dice:

(v1.2) pkg> up Arpack
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
 Installed AbstractFFTs ─ v0.5.0
  Updating `~/.julia/environments/v1.2/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.2/Manifest.toml`
  [621f4979] ↑ AbstractFFTs v0.4.1 ⇒ v0.5.0

(v1.2) pkg> build Arpack
  Building Arpack → `~/.julia/packages/Arpack/cu5By/deps/build.log`
┌ Error: Error building `Arpack`: 
...

also tried to update all packages, and also to install from master and build Arpack, no change (wasn’t addressing root issue):

(v1.2) pkg> up
(v1.2) pkg> add Arpack#master

So in linux shell, reinstalled Arpack from Arch repository to be safe (it was up to date, no change in building Arpack within Julia):

~/.../cu5By/deps >>> gcc --version                                                                                          
gcc (GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

~/.../cu5By/deps >>> sudo pacman -S arpack  
warning: arpack-3.7.0-2 is up to date -- reinstalling  

And finally, the resolution via a symlink:

$ sudo ln -s /usr/lib/libopenblas.so /usr/lib/libopenblas64_.so.0

I write this because other recommendations were to reinstall, ‘dev’ packages, and all sorts of other methods that would have probably made things worse. There is much discussion on this Arpack issue, and it took couple hours to read enough to understand what was going on. Hoping if you had this issue you found this note and it resolved for you quickly!

These two posts helped and offer great detail if interested:
The location where the command used to resolve was found (comment-437869878):


and comment-416065166

Posts: 1

Participants: 1

Read full topic


Use root privileges for a session/package?

$
0
0

@pontus wrote:

Wondering if there’s a simple way to run a Julia session, or use a specific package, with root privilege?
Would like to use the ODBC.jl package as sudo/root.
Am using Julia v1.2 and Linux (Arch-based platform with Manjaro 18.1.2/5.3.8-3-MANJARO x86_64)

Noted that I can run a Julia session as root, but that the packages from my regular user are no longer available:

$ sudo julia
julia> using ODBC
ERROR: ArgumentError: Package ODBC not found in current path:
- Run `import Pkg; Pkg.add("ODBC")` to install the ODBC package.
julia> exit()

Was going to give up there, but then saw this comment from @ExpandingMan in the “Discussion: Plans for Julia as a general-purpose language” thread:

So, thinking could either:

  1. run as root, add packages needed for the query/processing. Just worried about corrupting my general environment or other unforeseen problems.
  2. use the ]dev optioned mentioned by ExpandingMan. I just haven’t done that before.

Suggestions, or am I just asking for more trouble with any of this?

ps - I admit this is a temporary hack and not my “real ‘root’ problem”, which I believe to be improper install and configuration of my MariaDB install and config in linux, with the MariaDB data directory pointing to my home user so the mysql user can’t access, but until I can understand and deal with that, am hoping for a short-term fix, as already have a Julia workflow with a number of queries and data processing work already done from when using Win environment.

Funny, it seems a number of people have this permissions issue out in the wild apart from my case here, with other interfaces (not Julia problems), as the error message is same and scenario similar.
Trying to use the /MySQLMariaDB DSN works from a shell using root, but not in my Julia session:

Julia:

# not working yet, edit username / psswd; TODO figure out how to read this from a local file, ideally an encrypted one.
conn = ODBC.DSN("mariadb-dsn", "e", "XXXX")#update with your username and password. Can then edit and remove password after code block has run. 

errors with:

[ODBC] 08S01: [unixODBC][MySQL][ODBC 8.0(w) Driver]Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13)

ODBC.ODBCError("ODBC.API.SQLConnect(dbc, connectionstring, username, password) failed; return code: -1 => SQL_ERROR")

Stacktrace:
 [1] #DSN#3(::Bool, ::Type{ODBC.DSN}, ::String, ::String, ::String) at /home/e/.julia/packages/ODBC/YEzHX/src/ODBC.jl:57
 [2] ODBC.DSN(::String, ::String, ::String) at /home/e/.julia/packages/ODBC/YEzHX/src/ODBC.jl:119
 [3] top-level scope at In[7]:1

And using a local MySQL command line client (tried a couple, I like mycli as it offers syntax highlighting and auto-suggest in a shell environment!). These clients:

$ sudo mycli -u root
$ sudo mariadb -u root
$ sudo mysql -u root 

all connect fine

~ >>> sudo mycli -u root                                                                                                 [2]
mysql 10.4.8-MariaDB
mysql root@localhost:(none)> exit                                                                                            
Goodbye!

Also, the isql command line tool can connect:

~ >>> sudo isql -v mariadb-dsn                                                 
+---------------------------------------+
| Connected!                            |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
+---------------------------------------+
SQL> quit

BUT, running isql without root privilege give me the same error as within Julia the ODBC, hence, why I just want to get by for now running as root.

~ >>> isql -v mariadb-dsn                                                                                                   
[08S01][unixODBC][MySQL][ODBC 8.0(w) Driver]Can't connect to local MySQL server through socket '/tmp/mysql.sock' (13)
[ISQL]ERROR: Could not SQLConnect

Posts: 4

Participants: 3

Read full topic

Different Logging output between Juno Execute and Julia REPL

$
0
0

@venuur wrote:

For some reason @debug messages are appearing when I run a modules function via shift+enter in Juno with Julia 1.2 but not when I run the function directly on the REPL.

Here’s the minimal setup:

  1. Create new package via generate LoggerProblem at the julia repl.
  2. Open the new folder LoggerProblem in Juno.
  3. Change src/LoggerProblem.jl to contain the following:
module LoggerProblem

export log_example

function log_example()
    @debug "This statement should appear at debug level."
    @warn "This statement should appear at warn level."
end

end # module
  1. Create a new file scratch.jl in the package directory with the following contents:
import Pkg
Pkg.activate(".")
using Revise
using LoggerProblem
using Logging


logger = ConsoleLogger(stdout, Logging.Debug)
old_logger = global_logger(logger)

log_example()

If you run each line using shift+enter you get the following output.

┌ Debug: This statement should appear at debug level.
└ @ LoggerProblem C:\Users\carlm\workspace\julia-lisp\LoggerProblem\LoggerProblem\src\LoggerProblem.jl:6
┌ Warning: This statement should appear at warn level.
└ @ LoggerProblem C:\Users\carlm\workspace\julia-lisp\LoggerProblem\LoggerProblem\src\LoggerProblem.jl:7

However, if I run the next line I get different results:

julia> log_example()
┌ Warning: This statement should appear at warn level.
└ @ LoggerProblem C:\Users\carlm\workspace\julia-lisp\LoggerProblem\LoggerProblem\src\LoggerProblem.jl:7

I’ve read several posts and the documentation, but I can’t tell what’s going wrong. Any suggestions on where to look next for troubleshooting?

Posts: 1

Participants: 1

Read full topic

Using push!

$
0
0

@ennvvy wrote:

I am using push! to store the value of an array in every iteration t. But, it does not work like I expect it to:

function trial(n)
  m=Any[]
  y=rand(10)
  for t in 1:n
    push!(m,y)
    for x in 10
      y[x]=rand()
    end
  end
  return m
end

On executing the above function for n=2, I get:

2-element Array{Any,1}:
 [0.3413498610074548, 0.9008040663934613, 0.10721629384140297, 0.5546157202036206, 0.794439983649
3246, 0.729274144801793, 0.1932248146434996, 0.2950370824859381, 0.9389225778706747, 0.0149528983
28812081]
 [0.3413498610074548, 0.9008040663934613, 0.10721629384140297, 0.5546157202036206, 0.794439983649
3246, 0.729274144801793, 0.1932248146434996, 0.2950370824859381, 0.9389225778706747, 0.0149528983
28812081]

As can be seen, the last known value of y is stored in all of m instead of the different values of y in each iteration. Is there something wrong in my understanding?

Posts: 7

Participants: 4

Read full topic

Behavior of Base.maximum

$
0
0

@guberger wrote:

Hello! The following piece of code (example) prints 10 while I would have expected 5:

for i = 1:1
    it = 1:5
    f(t) = t
    b = maximum(f, it)
    f(t) = 2*t
    display(b)
end

Does someone have an explanation for that behavior? Thanks

Posts: 6

Participants: 4

Read full topic

Understanding assignment without allocation

$
0
0

@Rajesh_Talluri wrote:

I am trying to understand inplace assignment in julia.

In the following example I want to update a and b. The following code works, and it updates a and b in place without any allocations.

n = 1000
a = ones(n,1)
b = ones(n,1)
c = 1.0
d = 2.0
nsel = rand(n) .> 0.5

function testalloc!(a, b, c, d, nsel)
    @. a = c * a + d * b
    @. b = d * a + c * b
end

@btime testalloc!(a, b, c, d, nsel)
  891.646 ns (0 allocations: 0 bytes)

However, if i have to do it for a subset there are 28 allocations and it is slower


function testalloc1!(a, b, c, d, nsel)
    @. a[nsel] = c * a[nsel] + d * b[nsel]
    @. b[nsel] = d * a[nsel] + c * b[nsel]
end

@btime testalloc1!(a, b, c, d, nsel)
  3.350 μs (28 allocations: 24.22 KiB)

and if I wanted to do for the complementary subset there are 40 allocations… and it is even more slower


function testalloc2!(a, b, c, d, nsel)
    @. a[.!nsel] = c * a[.!nsel] + d * b[.!nsel]
    @. b[.!nsel] = d * a[.!nsel] + c * b[.!nsel]
end

@btime testalloc2!(a, b, c, d, nsel)
  3.775 μs (40 allocations: 27.13 KiB)

Could someone please help me understand the correct way to update subsets of arrays.

Thank You

Posts: 4

Participants: 3

Read full topic

Viewing all 2795 articles
Browse latest View live