# Factor multiplication values: incorrect reshape?

**URL:** <https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394>\
**Category:** Machine Learning\
**Created:** [January 10, 2017, 2:22pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394 "2017-01-10T14:22:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [January 10, 2017, 2:22pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394/1 "2017-01-10T14:22:51Z")

</div>

![](https://global.discourse-cdn.com/julialang/original/3X/7/7/77d9b4f387a02d631e79b3e135a92d2ee85a11fa.jpeg)

I can’t get Cvaluespace to output the correct values, identical to table 3.

 ![](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d494cec0e16ebe1e49d93f6cafef8aad5600df1.png)

I think it has to do with reshape, notebook is below.  
[https://github.com/hpoit/DFactor.jl/blob/master/function%20DFmultiply.ipynb](https://github.com/hpoit/DFactor.jl/blob/master/function%20DFmultiply.ipynb)

Thanks

---

<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:** [January 10, 2017, 4:34pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394/2 "2017-01-10T16:34:57Z")

</div>

Please don’t reproduce copyrighted material without acknowledging the source.

It is impossible to run your notebook without the `BN` module.

---

<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:** [January 10, 2017, 5:05pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394/3 "2017-01-10T17:05:41Z")

</div>

This seems to be a rather strange mathematical operation; I don’t even know the name for it.

Writing it in index notation, if `A` and `B` are the objects on the left and `M` the object on the right, we have

```julia
M[i, j, k] = A[i, j] * B[j, k]

```

Now we translate that to Julia:

```julia
julia> v1 = [0.5, 0.8, 0.1, 0, 0.3, 0.9]; # raw data

julia> v2 = [0.5, 0.7, 0.1, 0.2];

julia> A = reshape(v1, 2, 3)'
3×2 Array{Float64,2}:
 0.5 0.8
 0.1 0.0
 0.3 0.9

julia> B = reshape(v2, 2, 2)'
2×2 Array{Float64,2}:
 0.5 0.7
 0.1 0.2

julia> M = [A[i, j] * B[j, k] for i in 1:size(A,1), j in 1:size(A,2), k in 1:size(B,2)]
3×2×2 Array{Float64,3}:
[:, :, 1] =
 0.25 0.08
 0.05 0.0
 0.15 0.09

[:, :, 2] =
 0.35 0.16
 0.07 0.0
 0.21 0.18

```

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [January 10, 2017, 5:58pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394/4 "2017-01-10T17:58:14Z")

</div>

Thanks @dpsanders. The figure is from Prof. Daphne Koller’s Bayesian Networks, and the original code is from Wang Lei’s [DiscreteFactor.jl](https://github.com/wlbksy/DiscreteFactor.jl/blob/master/src/dfactor.jl).

---

<div class="post-metadata">

**Author:** ![tawheeler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tawheeler/32/17657_2.png) [@tawheeler](https://discourse.julialang.org/u/tawheeler)\
**Post date:** [January 11, 2017, 4:17pm UTC](https://discourse.julialang.org/t/factor-multiplication-values-incorrect-reshape/1394/5 "2017-01-11T16:17:24Z")

</div>

Keep in mind it needs to work for tables of arbitrary size. @hpoit should check that this works for tables with more variables and varied cardinalities.

There is a [new discrete factor implementation in BayesNets.jl](https://github.com/sisl/BayesNets.jl/tree/master/src/Factors) that may be of use.
