@@ -107,10 +107,14 @@ public class NDArray<T, D : Dimension> constructor(
107107 requirePositiveShape(dim1)
108108 require(dim1 == size) { " Cannot reshape array of size $size into a new shape ($dim1 )" }
109109
110+ // TODO(get rid of copying)
111+ val newData = if (consistent) this .data else this .deepCopy().data
112+ val newBase = if (consistent) base ? : this else null
113+
110114 return if (this .dim.d == 1 && this .shape.first() == dim1) {
111115 this as D1Array <T >
112116 } else {
113- D1Array (this .data , this .offset, intArrayOf(dim1), dim = D1 , base = base ? : this )
117+ D1Array (newData , this .offset, intArrayOf(dim1), dim = D1 , base = newBase )
114118 }
115119 }
116120
@@ -119,10 +123,14 @@ public class NDArray<T, D : Dimension> constructor(
119123 newShape.forEach { requirePositiveShape(it) }
120124 require(dim1 * dim2 == size) { " Cannot reshape array of size $size into a new shape ($dim1 , $dim2 )" }
121125
126+ // TODO(get rid of copying)
127+ val newData = if (consistent) this .data else this .deepCopy().data
128+ val newBase = if (consistent) base ? : this else null
129+
122130 return if (this .shape.contentEquals(newShape)) {
123131 this as D2Array <T >
124132 } else {
125- D2Array (this .data , this .offset, newShape, dim = D2 , base = base ? : this )
133+ D2Array (newData , this .offset, newShape, dim = D2 , base = newBase )
126134 }
127135 }
128136
@@ -131,10 +139,14 @@ public class NDArray<T, D : Dimension> constructor(
131139 newShape.forEach { requirePositiveShape(it) }
132140 require(dim1 * dim2 * dim3 == size) { " Cannot reshape array of size $size into a new shape ($dim1 , $dim2 , $dim3 )" }
133141
142+ // TODO(get rid of copying)
143+ val newData = if (consistent) this .data else this .deepCopy().data
144+ val newBase = if (consistent) base ? : this else null
145+
134146 return if (this .shape.contentEquals(newShape)) {
135147 this as D3Array <T >
136148 } else {
137- D3Array (this .data , this .offset, newShape, dim = D3 , base = base ? : this )
149+ D3Array (newData , this .offset, newShape, dim = D3 , base = newBase )
138150 }
139151 }
140152
@@ -143,10 +155,14 @@ public class NDArray<T, D : Dimension> constructor(
143155 newShape.forEach { requirePositiveShape(it) }
144156 require(dim1 * dim2 * dim3 * dim4 == size) { " Cannot reshape array of size $size into a new shape ($dim1 , $dim2 , $dim3 , $dim4 )" }
145157
158+ // TODO(get rid of copying)
159+ val newData = if (consistent) this .data else this .deepCopy().data
160+ val newBase = if (consistent) base ? : this else null
161+
146162 return if (this .shape.contentEquals(newShape)) {
147163 this as D4Array <T >
148164 } else {
149- D4Array (this .data , this .offset, newShape, dim = D4 , base = base ? : this )
165+ D4Array (newData , this .offset, newShape, dim = D4 , base = newBase )
150166 }
151167 }
152168
@@ -157,10 +173,14 @@ public class NDArray<T, D : Dimension> constructor(
157173 " Cannot reshape array of size $size into a new shape ${newShape.joinToString(prefix = " (" , postfix = " )" )} "
158174 }
159175
176+ // TODO(get rid of copying)
177+ val newData = if (consistent) this .data else this .deepCopy().data
178+ val newBase = if (consistent) base ? : this else null
179+
160180 return if (this .shape.contentEquals(newShape)) {
161181 this as NDArray <T , DN >
162182 } else {
163- NDArray (this .data , this .offset, newShape, dim = DN (newShape.size), base = base ? : this )
183+ NDArray (newData , this .offset, newShape, dim = DN (newShape.size), base = newBase )
164184 }
165185 }
166186
@@ -201,13 +221,11 @@ public class NDArray<T, D : Dimension> constructor(
201221 for (axis in axes.sorted()) {
202222 newShape.add(axis, 1 )
203223 }
204- return NDArray (
205- this .data,
206- this .offset,
207- newShape.toIntArray(),
208- dim = DN (newShape.size),
209- base = base ? : this
210- )
224+ // TODO(get rid of copying)
225+ val newData = if (consistent) this .data else this .deepCopy().data
226+ val newBase = if (consistent) base ? : this else null
227+
228+ return NDArray (newData, this .offset, newShape.toIntArray(), dim = DN (newShape.size), base = newBase)
211229 }
212230
213231 override infix fun cat (other : MultiArray <T , D >): NDArray <T , D > =
0 commit comments