# The \`perm\`of \`permutedims(A,perm)\`

**URL:** <https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572>\
**Category:** Machine Learning\
**Created:** [November 26, 2016, 12:24am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572 "2016-11-26T00:24:09Z")\
**Posts on this page:** 17\
**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:** [November 26, 2016, 12:24am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/1 "2016-11-26T00:24:10Z")

</div>

@yuyichao So you’re saying `perm` necessarily has to be an ordered basis and cannot be a set?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 12:43am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/2 "2016-11-26T00:43:39Z")

</div>

Well, that sounds reasonable because the unordered set is always the same anyway. I don’t remember when/where/in which context I said that though.

---

<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:** [November 26, 2016, 12:58am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/3 "2016-11-26T00:58:13Z")

</div>

You didn’t say it, but [this](https://groups.google.com/d/msg/julia-users/DPiwrJgtdvQ/YGXPuYV7CQAJ) is the context. If an ordered basis is the case, then [`permute_dims`](https://github.com/hpoit/MLN.jl/blob/master/BN/src/FactorOperations.jl#L66) is ordered. I don’t understand what’s missing for [`permutedims()`](https://github.com/hpoit/MLN.jl/blob/master/BN/src/FactorOperations.jl#L67) to work.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 5:29am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/4 "2016-11-26T05:29:54Z")

</div>

I don’t think I mentioned anything about ordering at all. The input type is simply wrong.

---

<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:** [November 26, 2016, 10:27am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/5 "2016-11-26T10:27:07Z")

</div>

Right, I brought up ordering. The input type is the same as the one from [`permutedims()` in this other block](https://github.com/hpoit/MLN.jl/blob/master/BN/src/FactorOperations.jl#L9), which passes. Should both `permutedims()` not be `Vector{Int}`? I don’t understand why not, from the definition of `perm` below

````julia
help?> permutedims
search: permutedims permutedims! ipermutedims

  permutedims(A, perm)

  Permute the dimensions of array A. perm is a vector specifying a permutation
  of length ndims(A). This is a generalization of transpose for
  multi-dimensional arrays. Transpose is equivalent to permutedims(A, [2,1]).
```
````

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 1:24pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/6 "2016-11-26T13:24:42Z")

</div>

> [@hpoit](#):
>
> The input type is the same as the one from permutedims() in this other block, which passes.

Well it isn’t.

> [@hpoit](#):
>
> Should both permutedims() not be Vector{Int}

Both should be.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 1:31pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/7 "2016-11-26T13:31:01Z")

</div>

> [@yuyichao](#):
>
> Well it isn’t.

And this is also why I posted the code in the one of the replies showing that the type is wrong.

---

<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:** [November 26, 2016, 1:54pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/8 "2016-11-26T13:54:45Z")

</div>

Do you mean ` [Remain_dims,Remove_dims]` is a 2x2 matrix? If yes, how do I turn it into an ordered vector? I’m sorry, thanks for the patience…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 2:03pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/9 "2016-11-26T14:03:12Z")

</div>

> [@hpoit](#):
>
> Do you mean [Remain\_dims,Remove\_dims] is a 2x2 matrix

No, it’s a `Vector` of `Vector`.

> [@hpoit](#):
>
> how do I turn it into an ordered vector

That’s exactly the code I posted, on the julia-users thread.

```julia
julia> [[1]]
1-element Array{Array{Int64,1},1}:
 [1]

julia> [[1];]
1-element Array{Int64,1}:
 1

```

And for more than one element to concatenate

```julia
julia> [[1]; [2, 3]]
3-element Array{Int64,1}:
 1
 2
 3

```

---

<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:** [November 26, 2016, 2:27pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/10 "2016-11-26T14:27:57Z")

</div>

Thanks, I switched it to `[Remain_dims;Remove_dims]`… but now I got this

````julia
julia> Pkg.test("BN")
INFO: Testing BN
WARNING: Method definition (::Type{BN.Factor})(Array{String, 1}, Array{Int64, 1}, Array{Float64, 1}) in module BN at /Users/Corvus/.julia/v0.5/BN/src/Factor.jl:9 overwritten at /Users/Corvus/.julia/v0.5/BN/src/Factor.jl:9.
Test Summary: | 
  Define Factor arguments, create instances, permute Factor | No tests
Multiply and marginalize factor: Error During Test
  Got an exception of type ArgumentError outside of a @test
  ArgumentError: reduced dimension(s) must be integers
   in reduced_dims(::Tuple{Int64,Int64,Int64}, ::Array{UnitRange{Int64},1}) at ./reducedim.jl:39
   in reducedim_initarray(::Array{Float64,3}, ::Array{UnitRange{Int64},1}, ::Float64, ::Type{Float64}) at ./reducedim.jl:85
   in mapreducedim at ./reducedim.jl:253 [inlined]
   in sum at ./reducedim.jl:303 [inlined]
   in sum(::Array{Float64,3}, ::Array{UnitRange{Int64},1}) at ./reducedim.jl:305
   in FactorMargin(::BN.Factor, ::Array{String,1}, ::Array{String,1}, ::Array{Int64,1}, ::Array{Int64,1}) at /Users/Corvus/.julia/v0.5/BN/src/FactorOperations.jl:71
   in FactorDropMargin(::BN.Factor, ::Array{String,1}) at /Users/Corvus/.julia/v0.5/BN/src/FactorOperations.jl:87
   in macro expansion; at /Users/Corvus/.julia/v0.5/BN/test/runtests.jl:19 [inlined]
   in macro expansion; at ./test.jl:672 [inlined]
   in anonymous at ./<missing>:?
   in include_from_node1(::String) at ./loading.jl:488
   in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
   in process_options(::Base.JLOptions) at ./client.jl:262
   in _start() at ./client.jl:318
   in _start() at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
Test Summary: | Error Total
  Multiply and marginalize factor | 1 1
ERROR: LoadError: Some tests did not pass: 0 passed, 0 failed, 1 errored, 0 broken.
 in finish(::Base.Test.DefaultTestSet) at ./test.jl:495
 in macro expansion; at ./test.jl:679 [inlined]
 in anonymous at ./<missing>:?
 in include_from_node1(::String) at ./loading.jl:488
 in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
 in process_options(::Base.JLOptions) at ./client.jl:262
 in _start() at ./client.jl:318
 in _start() at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
while loading /Users/Corvus/.julia/v0.5/BN/test/runtests.jl, in expression starting on line 14
=================================[ERROR: BN]==================================

failed process: Process(`/Applications/Julia-0.5.app/Contents/Resources/julia/bin/julia -Ccore2 -J/Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib --compile=yes --depwarn=yes --check-bounds=yes --code-coverage=none --color=yes --compilecache=yes /Users/Corvus/.julia/v0.5/BN/test/runtests.jl`, ProcessExited(1)) [1]

================================================================================
ERROR: BN had test errors
 in #test#61(::Bool, ::Function, ::Array{AbstractString,1}) at ./pkg/entry.jl:740
 in (::Base.Pkg.Entry.#kw##test)(::Array{Any,1}, ::Base.Pkg.Entry.#test, ::Array{AbstractString,1}) at ./<missing>:0
 in (::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#test,Tuple{Array{AbstractString,1}}})() at ./pkg/dir.jl:31
 in cd(::Base.Pkg.Dir.##2#3{Array{Any,1},Base.Pkg.Entry.#test,Tuple{Array{AbstractString,1}}}, ::String) at ./file.jl:59
 in #cd#1(::Array{Any,1}, ::Function, ::Function, ::Array{AbstractString,1}, ::Vararg{Array{AbstractString,1},N}) at ./pkg/dir.jl:31
 in (::Base.Pkg.Dir.#kw##cd)(::Array{Any,1}, ::Base.Pkg.Dir.#cd, ::Function, ::Array{AbstractString,1}, ::Vararg{Array{AbstractString,1},N}) at ./<missing>:0
 in #test#3(::Bool, ::Function, ::String, ::Vararg{String,N}) at ./pkg/pkg.jl:258
 in test(::String, ::Vararg{String,N}) at ./pkg/pkg.jl:258

julia> 
```

[Factor.jl:9](https://github.com/hpoit/MLN.jl/blob/master/BN/src/Factor.jl#L9)
[FactorOperations.jl:71](https://github.com/hpoit/MLN.jl/blob/master/BN/src/FactorOperations.jl#L71)
[FactorOperations.jl:87](https://github.com/hpoit/MLN.jl/blob/master/BN/src/FactorOperations.jl#L87)

Your help is much appreciated, Yichao.
````

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 26, 2016, 3:34pm UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/11 "2016-11-26T15:34:24Z")

</div>

> [@hpoit](#):
>
> ```julia
> in sum(::Array{Float64,3}, ::Array{UnitRange{Int64},1}) at ./reducedim.jl:305
> 
> ```

The backtrace was printed for a reason and you should use it to figure out how you are using the functions wrong. The method signature printed in this function that’s the first Base function you called in the backtrace should show clearly that you are giving `sum` the wrong type as `dims`. Note that the same wrong parameter is also passed to `squeeze` below, which expects a tuple.

---

<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:** [November 30, 2016, 1:26am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/12 "2016-11-30T01:26:34Z")

</div>

Thanks Yichao. I’m not sure if I should just sum the `permuted_valuespace` by the `Remain_dims` to make sense. Factor marginalization here is just the sum over the relevant variables (`Remain_var`).

To me this makes more sense, but I’m not sure

```julia
# Factor marginalization: computes the factor without the non-relevant variables (Remove_var)

function FactorMargin(A::Factor, Remove_var::Vector{String}, Remain_var::Vector{String}, Remove_dims::Vector{Int}, Remain_dims::Vector{Int})
    Remain_card = A.card[Remain_dims]

    valuespace = reshape(A.value, A.card...)
    permute_dims = [Remain_dims;Remove_dims]
    permuted_valuespace = permutedims(valuespace, permute_dims)

    # squeeze_dims = [length(Remain_dims)+1:length(permute_dims)] (I would delete this line)

    sumvaluespace = sum(permuted_valuespace, Remain_dims) # changed for Remain_dims
    Remain_valuespace = squeeze(sumvaluespace, Remove_dims) # changed for Remove_dims

    Factor(Remain_var, Remain_card, Remain_valuespace[:])
end

```

This is my [reference](https://github.com/liang456/Stanford-Probabilistic-Graphical-Models-Coursera/blob/master/PGM_Programming_Assignment_1/FactorMarginalization.m#L31-L35).  
[  
FactorProduct.m  
IndexToAssignment.m  
AssignmentToIndex.m](https://github.com/liang456/Stanford-Probabilistic-Graphical-Models-Coursera/tree/master/PGM_Programming_Assignment_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:** [November 30, 2016, 1:42am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/13 "2016-11-30T01:42:40Z")

</div>

Also, some days past I was having a bad one and I

````julia
julia> include("/Users/Corvus/.julia/v0.5/BN/src/BN.jl")
julia> include("/Users/Corvus/.julia/v0.5/BN/src/Factor.jl")
julia> include("/Users/Corvus/.julia/v0.5/BN/test/runtests.jl")

julia> Package.test("BN")
ERROR: UndefVarError: Package not defined
```
... even though the BN folder is already exists, in `.julia/v0.5/BN`. 

I don't know how to get back to using `.julia/v0.5/BN`.
````

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 30, 2016, 2:06am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/14 "2016-11-30T02:06:40Z")

</div>

> [@hpoit](#):
>
> To me this makes more sense, but I’m not sure

I’m pretty sure `squeeze` needs a tuple instead of a vector (and this operation is inherently type unstable if you pass in Vector as the dimensions)

Where did you get the `Package` from? Please read the actual error more carefully since I think this one should be pretty clear from the error message. The package module in base is called `Pkg`.  
Using `include` like that also won’t do anything about `Pkg.test`. The tests are run in a script and has nothing to do with the calling process.

---

<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:** [November 30, 2016, 2:31am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/15 "2016-11-30T02:31:08Z")

</div>

Sorry, I didn’t pay attention to `Package`.

The tuple is a non-issue, I agree. Does the logic of any version of the function make sense to you?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [November 30, 2016, 4:57am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/16 "2016-11-30T04:57:12Z")

</div>

> [@hpoit](#):
>
> Does the logic of any version of the function make sense to you?

Sorry that’s not what I’m familiar with.

---

<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:** [November 30, 2016, 11:55am UTC](https://discourse.julialang.org/t/the-perm-of-permutedims-a-perm/572/17 "2016-11-30T11:55:49Z")

</div>

That’s cool, thanks anyway!
