# How to get a matrix with the inverse of each element of another matrix?

**URL:** <https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616>\
**Category:** New to Julia\
**Tags:** question, matrices\
**Created:** [February 2, 2022, 2:20am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616 "2022-02-02T02:20:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casfooox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/casfooox/32/33408_2.png) [@Casfooox](https://discourse.julialang.org/u/Casfooox)\
**Post date:** [February 2, 2022, 2:20am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/1 "2022-02-02T02:20:31Z")

</div>

I’m new to Julia. Is there an easy way of getting a matrix with the inverses of each element in a matrix?  
So an element of the new matrix would be: B\_ij = 1/A\_ij

I have seen that at least in Julia 0.2.0 the following sintax was possible: B = 1/A  
However it seems to not work in Julia 1.6.4

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [February 2, 2022, 2:37am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/2 "2022-02-02T02:37:00Z")

</div>

You can use B = 1 ./ A  
Note the dot in front of “/”.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 2, 2022, 2:39am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/3 "2022-02-02T02:39:44Z")

</div>

Also `inv.(A)`.

(This is a bit more general than `1 ./ A`, e.g. it works even if the elements of `A` are invertible matrices.)

---

<div class="post-metadata">

**Author:** ![Casfooox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/casfooox/32/33408_2.png) [@Casfooox](https://discourse.julialang.org/u/Casfooox)\
**Post date:** [February 2, 2022, 2:42am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/4 "2022-02-02T02:42:42Z")

</div>

Thank you! I tried things like B = 1 /.(A) before  
Is there a reason why for most operators (I have seen so far) the dot is after the operator, while this one is before?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [February 2, 2022, 3:04am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/5 "2022-02-02T03:04:31Z")

</div>

Good question! For instance this leads to ambiguity

```julia
1.+x

```

[https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators](https://docs.julialang.org/en/v1/manual/mathematical-operations/#man-dot-operators)  
I believe that this wouldn’t

```julia
1+.x

```

I am guessing the reasons for the position of the dot are historical(?)

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [February 2, 2022, 8:11am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/6 "2022-02-02T08:11:37Z")

</div>

But

```julia
x+.1

```

would be ambiguous again…

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 2, 2022, 9:48am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/7 "2022-02-02T09:48:56Z")

</div>

> [@Casfooox](#):
>
> Is there a reason why for most operators (I have seen so far) the dot is after the operator, while this one is before?

This syntax was first implemented for infix operators. In this case the dot comes first, probably because of tradition from e.g. Matlab (`A .* B` vs `A * B`, etc.).

When the syntax was extended to functions, the choice of `.sin(A)` vs `sin.(A)` was made after long discussions, see

- [Alternative syntax for `map(func, x)` · Issue #8450 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/8450)
- [https://github.com/JuliaLang/julia/pull/15032](https://github.com/JuliaLang/julia/pull/15032)

Here are some particularly relevant comments from these discussions:

> [kmskire](https://github.com/JuliaLang/julia/issues/8450#issuecomment-56580847): When a module defines its own `sin` (or whatever) function and we want to use that function on a vector, do we do `Module..sin(v)` ? `Module.(.sin(v))` ? `Module.(.sin)(v)` ? `.Module.sin(v)` ?

> [stevengj](https://github.com/JuliaLang/julia/pull/15032#issuecomment-184402433): `.sin(x)` seems workable to me too, though it would be helpful to have an implementation of it to see what implications it has for the parser. An expression like `x.*.sin(x)` looks a bit harder to read than `x.*sin.(x)` , but not impossible.
> 
> A `.` prefix also makes `3.sin(x)` ambiguous between `3.0sin(x)` and `3 * .sin(x)` (a problem that we already have with `.^` etc.), whereas `sin.(x)` makes the parsing of the `.` unambiguous.
> 
> Note also that `.sin` already has a meaning via `import .sin` etcetera, so we would be overloading it.

> [stevengj](https://github.com/JuliaLang/julia/pull/15032#issuecomment-184812188): I don’t think we’re contemplating changing `.+` to `+.` etcetera; the former convention is too well-established. I agree the `.sin(x)` would be more consistent, it’s mainly a question of whether it would cause any parser oddness in the context of Julia’s existing syntax. The easiest way to determine this is to try to implement it…

> [nalimilan](https://github.com/JuliaLang/julia/pull/15032#issuecomment-185100428): If you read `.(` as the function call operator (as I mentioned above), then `f.(x)` and `.+` are consistent.

> [stevengj](https://github.com/JuliaLang/julia/pull/15032#issuecomment-185207903): My biggest concern with `.sin` is that things like `.cos(x).*.sin(x)` become hard to read because the `.*.` is hard to disentangle, unless you require a space.

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [February 2, 2022, 9:52am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/8 "2022-02-02T09:52:54Z")

</div>

> [@Bardo](#):
>
> Cheat sheet:
> 
> ```julia
> # Vectorizing
> @. expr # vectorize/fuse expression
> fun.(a) # function call
> a op. b # binary operator
> .op a # unary operator
> 
> ```

Your cheat sheet fails for `a ./ b` though.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 2, 2022, 10:34am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/10 "2022-02-02T10:34:15Z")

</div>

The `op.` is just wrong, it should be `.op` for all operators?

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [February 2, 2022, 10:55am UTC](https://discourse.julialang.org/t/how-to-get-a-matrix-with-the-inverse-of-each-element-of-another-matrix/75616/11 "2022-02-02T10:55:29Z")

</div>

Got confused, sorry.

```julia
# Vectorizing
@. expr # vectorize/fuse expressions
fun.(a) # function calls
.op a, a .op b # operators

```
