# Matrix operations with custom number type

**URL:** https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148
**Category:** General Usage
**Created:** [July 4, 2023, 3:24am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148 "2023-07-04T03:24:33Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)
#### Post date: [July 4, 2023, 3:24am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148/1 "2023-07-04T03:24:33Z")

</div>

Given this definition of a custom number type:

```julia
struct Node <: Number
end
export Node

Base.:*(a::Node, b::Node) = Node()
Base.:+(a::Node, b::Node) = Node()

```

why does this

```julia
julia> a = Node();b=Node();

julia> A = [a b;a b]
2×2 Matrix{Node}:
 Node() Node()
 Node() Node()

julia> bvec = [a,b]
2-element Vector{Node}:
 Node()
 Node()

julia> A*b
2×2 Matrix{Node}:
 Node() Node()
 Node() Node()

```

give a different result than this:

```julia
julia> [a b;a b]*[a,b]
ERROR: MethodError: no method matching Node(::Int64)

Closest candidates are:
  (::Type{T})(::T) where T<:Number
   @ Core boot.jl:792
  Node()
   @ MatrixX C:\tmp\Matrix\src\MatrixX.jl:4
  (::Type{T})(::AbstractChar) where T<:Union{AbstractChar, Number}
   @ Base char.jl:50
  ...

Stacktrace:
 [1-6] ⋮ internal
     @ Base, LinearAlgebra, Unknown
   [7] *(A::Matrix{Node}, x::Vector{Node})
     @ LinearAlgebra C:\Users\seatt\.julia\juliaup\julia-1.9.1+0.x64.w64.mingw32\share\julia\stdlib\v1.9\LinearAlgebra\src\matmul.jl:56
   [8] top-level scope
     @ REPL[12]:1
Use `err` to retrieve the full stack trace.

```

Is there some other Base method that has to be defined on Node to make this work?

---

<div class="post-metadata">

### Author: ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)
#### Post date: [July 4, 2023, 3:47am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148/2 "2023-07-04T03:47:48Z")

</div>

It looks like you’re multiplying `A` by `b` instead of `bvec`, which may be doing scalar multiplication instead of matrix-vector multiplication. Does `A * bvec` fail?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 4, 2023, 3:50am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148/3 "2023-07-04T03:50:40Z")

</div>

You need to define `Base.zero(::Node) = Node()`.

---

<div class="post-metadata">

### Author: ![brianguenter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brianguenter/32/29519_2.png) [@brianguenter](https://discourse.julialang.org/u/brianguenter)
#### Post date: [July 4, 2023, 4:25am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148/4 "2023-07-04T04:25:54Z")

</div>

my bad. A\*bvec fails with the same error message. Defining Base.zero solved the problem. That is an obscure error message though. Is there a doc section that details which Base methods should be defined for custom number types?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [July 4, 2023, 4:32am UTC](https://discourse.julialang.org/t/matrix-operations-with-custom-number-type/101148/5 "2023-07-04T04:32:17Z")

</div>

The error message definitely could be better. [Numbers · The Julia Language](https://docs.julialang.org/en/v1/base/numbers/#General-Number-Functions-and-Constants) has the functions that you might want to overload (note that for many number types a lot of them will not be applicable). I would recommend looking at the list of functions and seeing which make sense for your type and implementing enough of them to get them to work (many have sane fallbacks i.e. `iszero(x)` defaults to `isequal(x, zero(x))`).
