# Maximal value - product of two lists

**URL:** <https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 21, 2022, 7:26pm UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972 "2022-12-21T19:26:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [December 21, 2022, 7:26pm UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/1 "2022-12-21T19:26:24Z")

</div>

Concerning the following situation: max \forall i { h\_i \* X\_{mij} }

Is there a function to obtain the maximal value between the product of two lists?

---

<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:** [December 21, 2022, 7:37pm UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/2 "2022-12-21T19:37:18Z")

</div>

This?

```julia
julia> h = rand(10); x = rand(10,10,10);

julia> m = 2; j = 3;

julia> maximum(h .* x[m,:,j]) # note the dot in .* : element-wise multiplication
0.6705150233027053

julia> maximum(h[i] * x[m,i,j] for i in eachindex(h))
0.6705150233027053

```

The second won´t allocate intermediate arrays which are allocated in the first.

---

<div class="post-metadata">

**Author:** ![ParadaCarleton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paradacarleton/32/20005_2.png) [@ParadaCarleton](https://discourse.julialang.org/u/ParadaCarleton)\
**Post date:** [December 21, 2022, 8:50pm UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/3 "2022-12-21T20:50:43Z")

</div>

I’d also like to point out the generic version of this, `reduce`, which I think is easier to remember than the difference between `max` and `maximum`. `reduce(max, (h[i] * x[m, i, j] for i in eachindex(h)))` does the same thing, but works for generic “reducing” functions (functions that can take a collection and summarize it into one number). For example, `reduce(+, array)` will take an array and return the sum.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 21, 2022, 10:27pm UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/4 "2022-12-21T22:27:51Z")

</div>

[TensorCast.jl](https://github.com/mcabbott/TensorCast.jl) is a friend:

```julia
using TensorCast
maximum(@cast _[i] := h[i] * x[$m,i,$j]) 

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 22, 2022, 12:43am UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/5 "2022-12-22T00:43:54Z")

</div>

TensorCast is still a friend, with the `@reduce` macro, if you want to make the calculation for all `m` and `j`:

```julia
using TensorCast
@reduce S[m,j] := maximum(i) h[i] * x[m,i,j]

```

For example:

```julia
julia> h = rand(3)
3-element Vector{Float64}:
 0.8601614537887764
 0.9085586312619703
 0.5942727699394317

julia> x = rand(4,3,2)
4×3×2 Array{Float64, 3}:
[:, :, 1] =
 0.350826 0.739942 0.565042
 0.751966 0.430443 0.293777
 0.714827 0.974151 0.680083
 0.752213 0.289802 0.784696

[:, :, 2] =
 0.95478 0.0218679 0.652646 
 0.308032 0.499596 0.724832 
 0.426453 0.865667 0.790571 
 0.370235 0.494717 0.53362

julia> @reduce S[m,j] := maximum(i) h[i] * x[m,i,j]
4×2 Matrix{Float64}:
 0.672281 0.821265
 0.646812 0.453912
 0.885073 0.786509
 0.647025 0.44948

julia> S[1,1]
0.672280575421645

julia> S[1,1] == h[2]*x[1,2,1]
true

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 22, 2022, 9:48am UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/6 "2022-12-22T09:48:46Z")

</div>

The computation for single (m,j) could be also written as:

```julia
@reduce _ := maximum(i) h[i] * x[$m,i,$j]

```

---

<div class="post-metadata">

**Author:** ![baprata](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baprata/32/204269_2.png) [@baprata](https://discourse.julialang.org/u/baprata)\
**Post date:** [December 22, 2022, 10:41am UTC](https://discourse.julialang.org/t/maximal-value-product-of-two-lists/91972/7 "2022-12-22T10:41:33Z")

</div>

Dear colleagues, many thanks to you for you support.
