# <span class='text-muted'>047.</span> `reshape` with the optional `order` and `pad` arguments

<span style='font-size: small;' class='text-muted'>topic: {ref}`arrays`</span>

[`reshape`](https://gcc.gnu.org/onlinedocs/gfortran/RESHAPE.html)
returns an array with a different shape than the `source` array.
Optional `order` argument changes the order of elements of `source` from the usual column major.
Optional `pad` argument fills the result, with recycling, after the source is exhausted.

```{literalinclude} ../../src/reshape-array.f90
:language: fortran
:caption: reshape-array.f90 | <a href="https://github.com/zmoon/FortranTipBrowser/blob/main/src/reshape-array.f90" target="_blank" title="See this source on GitHub"><i class="fab fa-github"></i></a> | <a href="https://github.com/Beliavsky/FortranTip/blob/main/reshape_array.f90" target="_blank" title="See the original source on Beliavsky/FortranTip GitHub"><i class="fab fa-github"></i><sub>0</sub></a> | <a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:fortran,selection:(endColumn:1,endLineNumber:29,positionColumn:1,positionLineNumber:29,selectionStartColumn:1,selectionStartLineNumber:29,startColumn:1,startLineNumber:29),source:'program+test_reshape%0A+++implicit+none%0A%0A+++integer,+allocatable+::+v(:),+m(:,+:)%0A%0A+++v+%3D+%5B1,+2,+3,+4,+5,+6%5D%0A%0A+++print+*,+%22Fill+column-wise+(default):%22%0A+++m+%3D+reshape(source%3Dv,+shape%3D%5B2,+3%5D)%0A+++print+*,+m(1,+:)++!!+1+3+5%0A+++print+*,+m(2,+:)++!!+2+4+6%0A%0A+++print+*,+%22Fill+row-wise:%22%0A+++m+%3D+reshape(source%3Dv,+shape%3D%5B2,+3%5D,+order%3D%5B2,+1%5D)%0A+++print+*,+m(1,+:)++!!+1+2+3%0A+++print+*,+m(2,+:)++!!+4+5+6%0A%0A+++print+*,+%22Pad+with+%5B1,+-1%5D+after+exhausting+source+%5B0,+0%5D:%22%0A+++m+%3D+reshape(source%3D%5B0,+0%5D,+shape%3D%5B2,+3%5D,+order%3D%5B2,+1%5D,+pad%3D%5B1,+-1%5D)%0A+++print+*,+m(1,+:)++!!++0+0++1%0A+++print+*,+m(2,+:)++!!+-1+1+-1%0A%0A+++print+*,+%22Repeat+row-wise+example+supplying+%60pad%60+(unused):%22%0A+++!!+Here+%60pad%60+is+unused+because+%60size(source)%60+equals+%60product(shape)%60%0A+++m+%3D+reshape(source%3Dv,+shape%3D%5B2,+3%5D,+order%3D%5B2,+1%5D,+pad%3D%5B1,+-1%5D)%0A+++print+*,+m(1,+:)++!!++1+2+3%0A+++print+*,+m(2,+:)++!!++4+5+6%0A%0Aend+program+test_reshape%0A'),l:'5',n:'0',o:'Fortran+source+%231',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:gfortran112,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'0',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:fortran,libs:!(),options:'',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'x86-64+gfortran+11.2+(Fortran,+Editor+%231,+Compiler+%231)',t:'0')),k:50,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',m:62.300683371298405,n:'0',o:'',t:'0'),(g:!((h:output,i:(compiler:1,editor:1,fontScale:14,fontUsePx:'0',tree:'1',wrap:'1'),l:'5',n:'0',o:'Output+of+x86-64+gfortran+11.2+(Compiler+%231)',t:'0')),header:(),l:'4',m:37.699316628701595,n:'0',o:'',s:0,t:'0')),l:'3',n:'0',o:'',t:'0')),version:4" target="_blank" title="Open in Godbolt Compiler Explorer"><img src="https://raw.githubusercontent.com/compiler-explorer/compiler-explorer/main/views/resources/site-logo.svg" alt="Godbolt Compiler Explorer logo" width="55.11" height="16.7" class="align-text-bottom" /></a> | <a href="https://play.fortran-lang.org/?code=program%20test_reshape%0A%20%20%20implicit%20none%0A%0A%20%20%20integer%2C%20allocatable%20%3A%3A%20v%28%3A%29%2C%20m%28%3A%2C%20%3A%29%0A%0A%20%20%20v%20%3D%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D%0A%0A%20%20%20print%20%2A%2C%20%22Fill%20column-wise%20%28default%29%3A%22%0A%20%20%20m%20%3D%20reshape%28source%3Dv%2C%20shape%3D%5B2%2C%203%5D%29%0A%20%20%20print%20%2A%2C%20m%281%2C%20%3A%29%20%20%21%201%203%205%0A%20%20%20print%20%2A%2C%20m%282%2C%20%3A%29%20%20%21%202%204%206%0A%0A%20%20%20print%20%2A%2C%20%22Fill%20row-wise%3A%22%0A%20%20%20m%20%3D%20reshape%28source%3Dv%2C%20shape%3D%5B2%2C%203%5D%2C%20order%3D%5B2%2C%201%5D%29%0A%20%20%20print%20%2A%2C%20m%281%2C%20%3A%29%20%20%21%201%202%203%0A%20%20%20print%20%2A%2C%20m%282%2C%20%3A%29%20%20%21%204%205%206%0A%0A%20%20%20print%20%2A%2C%20%22Pad%20with%20%5B1%2C%20-1%5D%20after%20exhausting%20source%20%5B0%2C%200%5D%3A%22%0A%20%20%20m%20%3D%20reshape%28source%3D%5B0%2C%200%5D%2C%20shape%3D%5B2%2C%203%5D%2C%20order%3D%5B2%2C%201%5D%2C%20pad%3D%5B1%2C%20-1%5D%29%0A%20%20%20print%20%2A%2C%20m%281%2C%20%3A%29%20%20%21%20%200%200%20%201%0A%20%20%20print%20%2A%2C%20m%282%2C%20%3A%29%20%20%21%20-1%201%20-1%0A%0A%20%20%20print%20%2A%2C%20%22Repeat%20row-wise%20example%20supplying%20%60pad%60%20%28unused%29%3A%22%0A%20%20%20%21%20Here%20%60pad%60%20is%20unused%20because%20%60size%28source%29%60%20equals%20%60product%28shape%29%60%0A%20%20%20m%20%3D%20reshape%28source%3Dv%2C%20shape%3D%5B2%2C%203%5D%2C%20order%3D%5B2%2C%201%5D%2C%20pad%3D%5B1%2C%20-1%5D%29%0A%20%20%20print%20%2A%2C%20m%281%2C%20%3A%29%20%20%21%20%201%202%203%0A%20%20%20print%20%2A%2C%20m%282%2C%20%3A%29%20%20%21%20%204%205%206%0A%0Aend%20program%20test_reshape%0A" target="_blank" title="Open in Fortran Playground"><img src="https://raw.githubusercontent.com/fortran-lang/playground/main/frontend/src/fortran-logo.png" alt="Fortran logo" height="15.5" class="align-text-bottom" /></a>
```

```{code-block} text
:caption: Output[^gfortran-version]

 Fill column-wise (default):
           1           3           5
           2           4           6
 Fill row-wise:
           1           2           3
           4           5           6
 Pad with [1, -1] after exhausting source [0, 0]:
           0           0           1
          -1           1          -1
 Repeat row-wise example supplying `pad` (unused):
           1           2           3
           4           5           6

```

[^gfortran-version]: Compiled using `GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0` with no flags

---

<blockquote class="twitter-tweet"><p lang="en" dir="ltr">RESHAPE returns an array with a different shape than the SOURCE array. Optional ORDER argument changes the order of elements of SOURCE from the usual column major. Optional PAD argument fills the result, with recycling, after source is exhausted. <a href="https://t.co/Xb3RCZ6LcW">pic.twitter.com/Xb3RCZ6LcW</a></p>&mdash; FortranTip (@fortrantip) <a href="https://twitter.com/fortrantip/status/1474394605890244614?ref_src=twsrc%5Etfw">December 24, 2021</a></blockquote><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>