| 114 | """ |
| 115 | __array_priority__ = 10.0 |
| 116 | def __new__(subtype, data, dtype=None, copy=True): |
| 117 | warnings.warn('the matrix subclass is not the recommended way to ' |
| 118 | 'represent matrices or deal with linear algebra (see ' |
| 119 | 'https://docs.scipy.org/doc/numpy/user/' |
| 120 | 'numpy-for-matlab-users.html). ' |
| 121 | 'Please adjust your code to use regular ndarray.', |
| 122 | PendingDeprecationWarning, stacklevel=2) |
| 123 | if isinstance(data, matrix): |
| 124 | dtype2 = data.dtype |
| 125 | if (dtype is None): |
| 126 | dtype = dtype2 |
| 127 | if (dtype2 == dtype) and (not copy): |
| 128 | return data |
| 129 | return data.astype(dtype) |
| 130 | |
| 131 | if isinstance(data, N.ndarray): |
| 132 | if dtype is None: |
| 133 | intype = data.dtype |
| 134 | else: |
| 135 | intype = N.dtype(dtype) |
| 136 | new = data.view(subtype) |
| 137 | if intype != data.dtype: |
| 138 | return new.astype(intype) |
| 139 | if copy: return new.copy() |
| 140 | else: return new |
| 141 | |
| 142 | if isinstance(data, str): |
| 143 | data = _convert_from_string(data) |
| 144 | |
| 145 | # now convert data to an array |
| 146 | arr = N.array(data, dtype=dtype, copy=copy) |
| 147 | ndim = arr.ndim |
| 148 | shape = arr.shape |
| 149 | if (ndim > 2): |
| 150 | raise ValueError("matrix must be 2-dimensional") |
| 151 | elif ndim == 0: |
| 152 | shape = (1, 1) |
| 153 | elif ndim == 1: |
| 154 | shape = (1, shape[0]) |
| 155 | |
| 156 | order = 'C' |
| 157 | if (ndim == 2) and arr.flags.fortran: |
| 158 | order = 'F' |
| 159 | |
| 160 | if not (order or arr.flags.contiguous): |
| 161 | arr = arr.copy() |
| 162 | |
| 163 | ret = N.ndarray.__new__(subtype, shape, arr.dtype, |
| 164 | buffer=arr, |
| 165 | order=order) |
| 166 | return ret |
| 167 | |
| 168 | def __array_finalize__(self, obj): |
| 169 | self._getitem = False |