# How to upgrade code for a symmetric distance matrix from julia-1.0 to julia-1.6

**URL:** <https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302>\
**Category:** General Usage\
**Created:** [March 2, 2022, 2:04pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302 "2022-03-02T14:04:42Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 2, 2022, 2:04pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/1 "2022-03-02T14:04:42Z")

</div>

As part of my code for julia-1.6 I try to calculate an NxN symmetric ‘distance’ matrix, where ‘distance’ is a function of 4 vectors (extracted as columns from a DataFrame),and two parameters (‘R2’ and ‘range’).  
While the code below worked with julia-1.0, it now gets stuck without an error message.  
( I checked the ‘distance’ function separately; nothing wrong with it.)

```julia
for i = 1:(N-1)
  for j = (i+1):N
    d2[i,j] = ((z_pred[i] - z_pred[j])^2)/R2 + (s2[i] + s2[j]).*(1 - exp.(-3*(sqrt.((x[i] - x[j])^2 + (y[i] - y[j])^2)/range)))
  end
end
d2 = d2 + d2'

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 2, 2022, 2:07pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/2 "2022-03-02T14:07:47Z")

</div>

What is `N`? What is `d2`? What is `z_pred`? I’ll stop there, my point is basically make an MWE so that people can reproduce your issues, and ideally read:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 2, 2022, 6:12pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/3 "2022-03-02T18:12:42Z")

</div>

Sorry for not putting my question in the right way. I’ll try to do it better now by providing a simplified version of the problematic part of my code. See below.

```julia
# A simplified version of my calculation of a distance matrix 'd2':
N = 5
d2 = zeros(N,N)
R2 = 0.36
range = 582
z_pred = (1, 8, 3, 2, 4)
s2 = (0.4, 0.2, 0.3, 0.9, 0.1)
x = (-50, 30, 12, -3, 19)
y = (7, 3.5, 4.2, 10, -25)
for i = 1:(N-1)
  for j = (i+1):N
    d2[i,j] = ((z_pred[i] - z_pred[j])^2)/R2 + (s2[i] + s2[j]).*(1 - exp.(-3*(sqrt.((x[i] - x[j])^2 + (y[i] - y[j])^2)/range)))
  end
end
d2 = d2 + d2'
println(d2)

```

To my surprise this small example worked and produced a good 5x5 matrix.  
So I’ve still no idea why the real code doesn’t work.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 2, 2022, 6:32pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/4 "2022-03-02T18:32:48Z")

</div>

> [@jjdegruijter](#):
>
> To my surprise this small example worked and produced a good 5x5 matrix.  
> So I’ve still no idea why the real code doesn’t work.

This is the reason MWE’s are so important, because they teach the person afflicted by the problem that their problem is not where they think it is, before they rope other people into finding it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 2, 2022, 8:44pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/5 "2022-03-02T20:44:32Z")

</div>

I wouldn’t be that harsh. But MWEs are great! They make your questions better, and often solve the problem outright.

I’ll add one piece of advice: wrap your code in a function. It makes the code more self-contained and performant, and removes problems caused by various hidden state you may have hanging around your workspace.

A bare loop hanging out there exposed in the global scope, looks totally unnatural and ugly to me now.

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 2, 2022, 9:24pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/6 "2022-03-02T21:24:07Z")

</div>

Do you mean all the code in one function? What I have now is 4 files, say A, B, C and D. The codes in B, C and D are each wrapped in a function.  
Any run follows A \> B \> C or A \> B \> D, depending on a parameter setting in A.

This is the reason why I did not see how to make a MWE

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 2, 2022, 10:06pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/7 "2022-03-02T22:06:59Z")

</div>

I meant put the main part of your MWE in a function.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 2, 2022, 10:17pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/8 "2022-03-02T22:17:57Z")

</div>

> [@jjdegruijter](#):
>
> So I’ve still no idea why the real code doesn’t work.

You could still show us the error message at least?

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 3, 2022, 12:26am UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/9 "2022-03-03T00:26:40Z")

</div>

As others have told, without further information is difficult to know with certainty, but let’s try a guess:

If the error really happens in the code you have posted, it may be either that:

- The vectors used inside the loop cannot be indexed with the `i`, `j` values that are defined (e.g. they have less than `N` values).
- Same with the matrix `d2` (it is smaller than `N`×`N`).
- Some variable used in the loop does not exist.

Or maybe the error does not happen there?

> [@jjdegruijter](#):
>
> As part of my code for julia-1.6 I try to calculate an NxN symmetric ‘distance’ matrix, where ‘distance’ is a function of 4 vectors (extracted as columns from a DataFrame),and two parameters (‘R2’ and ‘range’).

So you’re at least using the DataFrames package. Maybe it’s not the older version of Julia what gives you problems, but the version of that package. How do you extract the columns? Long ago you could do `dataframe[columnname]`, in current versions that’s not allowed, and you have to write `dataframe[!, columnname]` or `dataframe[:, columname]` (copy).

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 3, 2022, 12:53pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/10 "2022-03-03T12:53:15Z")

</div>

Part of the problem is that there isn’t any error message. Julia simply halts

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 3, 2022, 1:00pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/11 "2022-03-03T13:00:35Z")

</div>

The way to debug something like this can be:

1. Be sure that you don’t have _any_ global variable anywhere in your functions, particularly that one that halts. This is a good advice not only because of the peculiarity of the Julia performance that depends on this, but as a general advice in any programming language - it allows the localization of the problem much more easily.

2. Make a copy of the code and and try o remove as much as code as you can while keeping the error. That will produce a MWE, which may be minimal enough to be shared, or at least will help you to localize the problem.

3. Try to split the code into smaller functions, being sure that none of these functions depend on global variables. This is a very good programming practice as well, because then, if your program halts in one of this specific functions, and it is dependent only the exact input arguments, you can localize the problem in the operations of that function.

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 3, 2022, 1:20pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/12 "2022-03-03T13:20:17Z")

</div>

> - The vectors used inside the loop cannot be indexed with the `i`, `j` values that are defined (e.g. they have less than `N` values).
> - Same with the matrix `d2` (it is smaller than `N`×`N`).

I have checked this before, by printing i and j within the loop. At the end of the (very long) list came i = 4381 and j = 4382, as it should.

> - Some variable used in the loop does not exist.

I have checked this also before, by doing the calculation of one generalised distance. That went well, so all variables do exist.

> Or maybe the error does not happen there?

> I tried to locate the error in the way I’m used to: by printing a short message (“untill here”) at crucial places.

In this case there was a message just before the loop, but not after it.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 3, 2022, 1:27pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/13 "2022-03-03T13:27:43Z")

</div>

> [@jjdegruijter](#):
>
> Part of the problem is that there isn’t any error message. Julia simply halts

This is still ambigous as in

- returns to the REPL prompt (then you could investigate global variable state)
- aborts to the OS shell (then I’d expect to see some kind of crash dump)
- returns to the OS shell (then I’d expect to see some kind of call to `exit` in your code)
- diverges, endless loop (then I’d expect to see at least one Julia thread active in your system monitor)
- diverges due to dead lock

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 3, 2022, 1:40pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/14 "2022-03-03T13:40:23Z")

</div>

I think your possibillity nr 4 is the case, as I seen in CALL Stack “running"

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [March 3, 2022, 1:44pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/15 "2022-03-03T13:44:45Z")

</div>

In this case I’d instrument the program with `println` or `show` to monitor progress or would use a debugger to step through it.

Edit: I’m not sure if a screenshot would help us to better understand the problem?

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 3, 2022, 6:56pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/16 "2022-03-03T18:56:14Z")

</div>

I have to apologize, because some minutes ago it appeared that the problem wasn’t in the distance matrix calculation, but in a vector declaration close after the matrix piece of code.  
I did have println(“till here”) directly after matrix code, but that didn’t show up. The reason must have been that I have forgotten to save a change before running.  
Now that I ran the code it did show up.  
Please accept my appology.

---

<div class="post-metadata">

**Author:** ![jjdegruijter](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jjdegruijter](https://discourse.julialang.org/u/jjdegruijter)\
**Post date:** [March 3, 2022, 7:02pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/17 "2022-03-03T19:02:15Z")

</div>

I have to apologize, because some minutes ago it appeared that the problem wasn’t in the distance matrix calculation, but in a vector declaration close after the matrix piece of code.  
I did have println(“till here”) directly after matrix code, but that didn’t show up. The reason must have been that I have forgotten to save a change before running.  
Now that I ran the code it did show up.  
Please accept my appology.

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [March 3, 2022, 9:11pm UTC](https://discourse.julialang.org/t/how-to-upgrade-code-for-a-symmetric-distance-matrix-from-julia-1-0-to-julia-1-6/77302/18 "2022-03-03T21:11:04Z")

</div>

> [@jjdegruijter](#):
>
> I tried to locate the error in the way I’m used to: by printing a short message (“untill here”) at crucial places.

Glad to know that you eventually found the error. As an extra piece of advice, I recommend you to use the nice logging tools that Julia has to manage those “debugging” messages: instead of

```julia
println("until here")

```

you can write this in your code

```julia
@debug "until here"

```

By default those messages won’t show up, but you can “turn them on” with the following instruction:

```julia
ENV["JULIA_DEBUG"] = Main

```

And when you feel like disabling them again, just do:

```julia
ENV["JULIA_DEBUG"] = ""

```

A bonus of doing it like this is that you don’t have to redefine the functions to clear the debugging messages, and that’s good for performance.

More detailed information here:  
[https://docs.julialang.org/en/v1/stdlib/Logging/](https://docs.julialang.org/en/v1/stdlib/Logging/)
