# Equivalent of numpy.ascontiguousarray in Julia

**URL:** <https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525>\
**Category:** General Usage\
**Created:** [June 28, 2017, 7:30pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525 "2017-06-28T19:30:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 28, 2017, 7:30pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/1 "2017-06-28T19:30:26Z")

</div>

I would like to change a non-contiguous array to a contiguous one. What is the fastest way to do that in Julia?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [June 28, 2017, 7:57pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/2 "2017-06-28T19:57:44Z")

</div>

`convert(Array,A)` should work on objects which `<:AbstractArray`.

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 28, 2017, 8:51pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/3 "2017-06-28T20:51:28Z")

</div>

Seems like it is not working. I am not sure if you can reproduce the problem I am getting, but here it is anyways.

I am trying to use the numpy\_to\_vtk function from vtk.numpy\_support through PyCall to change a Julia array to a vtk array. This function only accepts C contiguous arrays as input. Now even when trying to call np.array through PyCall, the returned array is a Julia array, and using np.ascontiguousarray through PyCall also doesn’t do the trick as shown by the following repeating error. The error persists even when using np.ascontiguousarray(julia\_array) and np.array(convert(Array, julia\_array)) before passing the array to numpy\_to\_vtk.

```julia
ERROR: PyError (:PyObject_Call) <type 'exceptions.AssertionError'>
AssertionError('Only contiguous arrays are supported.',)
  File "C:\Program Files\VTK 7.1.1\bin\Lib\site-packages\vtk\util\numpy_support.
py", line 131, in numpy_to_vtk
    assert z.flags.contiguous, 'Only contiguous arrays are supported.'

```

Perhaps the question can be rephrased to how to make a C contiguous array inside Julia?

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 28, 2017, 9:21pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/4 "2017-06-28T21:21:05Z")

</div>

Can you post a reproducible example?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 28, 2017, 9:29pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/5 "2017-06-28T21:29:42Z")

</div>

```julia
using PyCall
@pyimport numpy as np
@pyimport vtk.util.numpy_support as vtkns

julia_array = rand(3,3);
vtk_array = vtkns.numpy_to_vtk(np.array(julia_array))

```

Edited to add:

Using PyObject explicitly creates a proper numpy array with access to its fields and methods. I am interested in making a row-major or C contiguous array not an F contiguous array as shown by the flags.

```julia
julia> numpy_array = PyObject(np.array(rand(3,3)))
PyObject array([[0.55107837, 0.98646111, 0.80035289],
       [0.92075771, 0.08981173, 0.88408335],
       [0.50036782, 0.91732487, 0.65456648]])

julia> numpy_array[:flags]
PyObject C_CONTIGUOUS : False
  F_CONTIGUOUS : True
  OWNDATA : False
  WRITEABLE : True
  ALIGNED : True
  UPDATEIFCOPY : False

```

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 28, 2017, 9:50pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/6 "2017-06-28T21:50:19Z")

</div>

`vtk_array = vtkns.numpy_to_vtk(PyReverseDims(julia_array))`

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 28, 2017, 9:51pm UTC](https://discourse.julialang.org/t/equivalent-of-numpy-ascontiguousarray-in-julia/4525/7 "2017-06-28T21:51:45Z")

</div>

Perfect, thanks!

ETA: Except when I get the array back in Julia using vtk\_to\_numpy, it is trasposed, which is easy to work around, but worth mentioning.
