# Linear Algebra with custom scalar product

**URL:** <https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836>\
**Category:** General Usage\
**Created:** [June 5, 2020, 10:23pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836 "2020-06-05T22:23:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [June 5, 2020, 10:23pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/1 "2020-06-05T22:23:45Z")

</div>

Borrowing an example from the popular [talk](https://www.youtube.com/watch?v=kc9HwsxE1OY), let’s say I want to use `LinearAlgebra.qr()` , but with three-argument `dot()` introduced in Julia 1.4. So I need to define my own CustomVector that carries within it the middle argument `A` for three-argument dot product (and redefine `reflector!` because it assumes normal Euclidean scalar product). But arrays are not bit types, therefore CustomVector may not be parametrized by `A`. So it looks like the only way is to have a global `A`, common for all instances of CustomVector.

Is there some better way?

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [June 5, 2020, 11:22pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/2 "2020-06-05T23:22:19Z")

</div>

I’m not sure I entirely understand the usecase, but maybe something like this:

```julia
struct CustomVector{T, TA} <: AbstractVector{T}
    data::Vector{T}
    A::TA
end

```

(You would also want to implement the AbstractArray interface if you subtype `AbstractVector`: [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-array-1)).

You don’t need to put `A` as a type parameter to use it, e.g.

```julia
my_dot(v::CustomVector, u::AbstractVector) = dot(v.data, v.A, u)

```

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [June 5, 2020, 11:24pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/3 "2020-06-05T23:24:37Z")

</div>

There should be some way to prevent multiplication of instances with different `A`

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 6, 2020, 1:21am UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/4 "2020-06-06T01:21:23Z")

</div>

I thought I remembered someone (@Mason?) showing recently how to locally redefine a function inside a `let` block. Maybe you could locally redefine `dot`?

---

<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:** [June 6, 2020, 3:35am UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/5 "2020-06-06T03:35:46Z")

</div>

> [@Mikhail\_Kagalenko](#):
>
> There should be some way to prevent multiplication of instances with different `A`

You could just have a runtime check and throw an error if `v.A !== u.A` (`!==` checks object identity).

> [@dpsanders](#):
>
> I thought I remembered someone (@Mason?) showing recently how to locally redefine a function inside a `let` block. Maybe you could locally redefine `dot` ?

No, that’s the same as defining a local variable that shadows a global variable. A local definition of `dot` would not be visible inside the `qr` function.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [June 6, 2020, 10:59am UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/6 "2020-06-06T10:59:11Z")

</div>

> [@stevengj](#):
>
> You could just have a runtime check and throw an error if `v.A !== u.A` ( `!==` checks object identity).

You probably mean `===`, because `==` is doing elementwise comparison of the arrays.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [June 6, 2020, 12:03pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/7 "2020-06-06T12:03:27Z")

</div>

No, `!==` is the negation of `===`, the negation of `==` is `!=`.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Kagalenko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikhail_kagalenko/32/13257_2.png) [@Mikhail\_Kagalenko](https://discourse.julialang.org/u/Mikhail_Kagalenko)\
**Post date:** [June 6, 2020, 6:33pm UTC](https://discourse.julialang.org/t/linear-algebra-with-custom-scalar-product/40836/8 "2020-06-06T18:33:01Z")

</div>

I stand corrected. It would probably be necessary to specialise `deepcopy()` to avoid dupicating `A`.

Another possible implementation would parameterize CustomVector with pointer to `A`. It would then be necessary to wrap the computation in `@GC.preserve A`. Not sure it would’ve any advantages over comparing references to A.
