# How to write generic low-rank updates?

**URL:** https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370
**Category:** General Usage
**Tags:** linearalgebra
**Created:** [September 10, 2020, 10:43am UTC](https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370 "2020-09-10T10:43:16Z")
**Posts on this page:** 4
**Page:** 1

<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: [September 10, 2020, 10:43am UTC](https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370/1 "2020-09-10T10:43:16Z")

</div>

I have to make computations with a low-rank update of a matrix A+uv.

Is there a way to modify the statement

```julia
x = A*w + u * (v*w)

```

so that it works generically for all reasonable types? `u,v` could be a vector and its adjoint, or two matrices of compatible size. In Matlab it would work out of the box because “everything has two indices” there, but in Julia, as it is written, for instance it fails for

```julia
A = randn(5, 5)
u = randn(5)
v = abs.(randn(5)')
w = randn(5)

```

because `v` is an `Array{Float64,2}`, so the product in parentheses returns an `Array{Float64,1}` and the product outside parentheses fails with

```julia
ERROR: MethodError: no method matching *(::Array{Float64,1}, ::Array{Float64,1})

```

Ideally, `x` should be a `Vector` if `w` is a `Vector`, and a `Matrix` if `w` is a `Matrix`.

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [September 10, 2020, 10:51am UTC](https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370/2 "2020-09-10T10:51:41Z")

</div>

The problem here is that `abs.(randn(5)')` doesn’t have the same type as `randn(5)'` and returns a matrix instead of an adjoint. I guess that’s a problem with broadcasting which should preserve the input type here?

---

<div class="post-metadata">

### Author: ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)
#### Post date: [September 10, 2020, 10:54am UTC](https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370/3 "2020-09-10T10:54:44Z")

</div>

Note that `map(abs, randn(5)')` preserves the Adjoint-ness.

---

<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: [September 10, 2020, 11:15am UTC](https://discourse.julialang.org/t/how-to-write-generic-low-rank-updates/46370/4 "2020-09-10T11:15:08Z")

</div>

Thanks to both! I am not expert enough to tell if this is a problem with broadcasting that should be fixed at the language level or if it’s just me doing it wrong. But in any case switching to `map` worked for my use case.
