Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

# -*- coding: utf-8 -*- 

# This file is part of the pyMOR project (http://www.pymor.org). 

# Copyright Holders: Rene Milk, Stephan Rave, Felix Schindler 

# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause) 

 

from __future__ import absolute_import, division, print_function 

 

from numbers import Number 

 

import numpy as np 

from scipy.sparse import issparse 

 

from pymor.core import NUMPY_INDEX_QUIRK 

from pymor.la.interfaces import VectorArrayInterface 

from pymor.tools import float_cmp 

 

 

class NumpyVectorArray(VectorArrayInterface): 

    '''|VectorArray| implementation via |NumPy arrays|. 

 

    This is the default |VectorArray| type used by all |Operators| 

    implemented directly in pyMOR. Reduced |Operators| will also 

    expect |NumpyVectorArrays|. 

 

    Note that this class is just thin wrapper around the underlying 

    |NumPy array|. Thus, while operations like 

    :meth:`~VectorArrayInterface.axpy` or :meth:`VectorArrayInterface.dot` 

    will be quite efficient, removing or appending vectors will 

    be costly. 

    ''' 

 

    @classmethod 

    def empty(cls, dim, reserve=0): 

        va = cls(np.empty((0, 0))) 

        va._array = np.empty((reserve, dim)) 

        va._len = 0 

        return va 

 

    @classmethod 

    def zeros(cls, dim, count=1): 

        return cls(np.zeros((count, dim))) 

 

    def __init__(self, instance, dtype=None, copy=False, order=None, subok=False): 

        if isinstance(instance, np.ndarray): 

            if copy: 

                self._array = instance.copy() 

            else: 

                self._array = instance 

        elif hasattr(instance, 'data'): 

            self._array = instance.data 

            if copy: 

                self._array = self._array.copy() 

        elif issparse(instance): 

            self._array = np.array(instance.todense(), copy=False) 

        else: 

            self._array = np.array(instance, dtype=dtype, copy=copy, order=order, subok=subok, ndmin=2) 

        if self._array.ndim != 2: 

            assert self._array.ndim == 1 

            self._array = np.reshape(self._array, (1, -1)) 

        self._len = len(self._array) 

 

    @property 

    def data(self): 

        return self._array[:self._len] 

 

    def __len__(self): 

        return self._len 

 

    @property 

    def dim(self): 

        return self._array.shape[1] 

 

    def copy(self, ind=None): 

        assert self.check_ind(ind) 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            return NumpyVectorArray(self._array[:0], copy=True) 

 

        if ind is None: 

            return NumpyVectorArray(self._array[:self._len], copy=True) 

        else: 

            C = NumpyVectorArray(self._array[ind], copy=False) 

            if not C._array.flags['OWNDATA']: 

                C._array = np.array(C._array) 

            return C 

 

    def append(self, other, o_ind=None, remove_from_other=False): 

        assert other.check_ind(o_ind) 

        assert self.dim == other.dim 

        assert other is not self or not remove_from_other 

 

        if NUMPY_INDEX_QUIRK and other._len == 0: 

            o_ind = None 

 

        if o_ind is None: 

            len_other = other._len 

            if len_other <= self._array.shape[0] - self._len: 

                self._array[self._len:self._len + len_other] = other._array 

            else: 

                self._array = np.vstack((self._array[:self._len], other._array[:len_other])) 

            self._len += len_other 

        else: 

            if not hasattr(o_ind, '__len__'): 

                len_other = 1 

                o_ind = [o_ind] 

            else: 

                len_other = len(o_ind) 

            if len_other <= self._array.shape[0] - self._len: 

                other._array.take(o_ind, axis=0, out=self._array[self._len:self._len + len_other]) 

            else: 

                self._array = np.append(self._array[:self._len], other._array[o_ind], axis=0) 

            self._len += len_other 

        if remove_from_other: 

            other.remove(o_ind) 

 

    def remove(self, ind=None): 

        assert self.check_ind(ind) 

 

        if ind is None: 

            self._array = np.zeros((0, self.dim)) 

            self._len = 0 

        else: 

            if hasattr(ind, '__len__'): 

                if len(ind) == 0: 

                    return 

                remaining = sorted(set(xrange(len(self))) - set(ind)) 

                self._array = self._array[remaining] 

            else: 

                assert -self._len < ind < self._len 

                self._array = self._array[range(ind) + range(ind + 1, self._len)] 

            self._len = self._array.shape[0] 

        if not self._array.flags['OWNDATA']: 

            self._array = self._array.copy() 

 

    def replace(self, other, ind=None, o_ind=None, remove_from_other=False): 

        assert self.check_ind_unique(ind) 

        assert other.check_ind(o_ind) 

        assert self.dim == other.dim 

        assert other is not self or not remove_from_other 

 

        if NUMPY_INDEX_QUIRK: 

            if self._len == 0 and hasattr(ind, '__len__'): 

                ind = None 

            if other._len == 0 and hasattr(o_ind, '__len__'): 

                o_ind = None 

 

        if ind is None: 

            if o_ind is None: 

                if other is self: 

                    return 

                assert other._len == self._len 

                self._array = other._array[:other._len].copy() 

            else: 

                if not hasattr(o_ind, '__len__'): 

                    o_ind = [o_ind] 

                assert self._len == len(o_ind) 

                self._array = other._array[o_ind] 

            self._len = self._array.shape[0] 

        else: 

            len_ind = self.len_ind(ind) 

            other_array = np.array(self._array) if other is self else other._array 

            if o_ind is None: 

                assert len_ind == other._len 

                self._array[ind] = other_array[:other._len] 

            else: 

                len_oind = other.len_ind(o_ind) 

                assert len_ind == len_oind 

                self._array[ind] = other_array[o_ind] 

        assert self._array.flags['OWNDATA'] 

 

        if remove_from_other: 

            other.remove(o_ind) 

 

    def almost_equal(self, other, ind=None, o_ind=None, rtol=None, atol=None): 

        assert self.check_ind(ind) 

        assert other.check_ind(o_ind) 

        assert self.dim == other.dim 

 

        if NUMPY_INDEX_QUIRK: 

            if self._len == 0 and hasattr(ind, '__len__'): 

                ind = None 

            if other._len == 0 and hasattr(o_ind, '__len__'): 

                o_ind = None 

 

        A = self._array[:self._len] if ind is None else \ 

            self._array[ind] if hasattr(ind, '__len__') else self._array[ind:ind + 1] 

        B = other._array[:other._len] if o_ind is None else \ 

            other._array[o_ind] if hasattr(o_ind, '__len__') else other._array[o_ind:o_ind + 1] 

 

        R = np.all(float_cmp(A, B, rtol=rtol, atol=atol), axis=1).squeeze() 

        if R.ndim == 0: 

            R = R[np.newaxis, ...] 

        return R 

 

    def scal(self, alpha, ind=None): 

        assert self.check_ind_unique(ind) 

        assert isinstance(alpha, Number) 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            return 

 

        if ind is None: 

            self._array[:self._len] *= alpha 

        else: 

            self._array[ind] *= alpha 

 

    def axpy(self, alpha, x, ind=None, x_ind=None): 

        assert self.check_ind_unique(ind) 

        assert x.check_ind(x_ind) 

        assert self.dim == x.dim 

        assert self.len_ind(ind) == x.len_ind(x_ind) 

 

        if NUMPY_INDEX_QUIRK: 

            if self._len == 0 and hasattr(ind, '__len__'): 

                ind = None 

            if x._len == 0 and hasattr(x_ind, '__len__'): 

                x_ind = None 

 

        if alpha == 0: 

            return 

 

        B = x._array[:x._len] if x_ind is None else x._array[x_ind] 

 

        if alpha == 1: 

            if ind is None: 

                self._array[:self._len] += B 

            else: 

                self._array[ind] += B 

        elif alpha == -1: 

            if ind is None: 

                self._array[:self._len] -= B 

            else: 

                self._array[ind] -= B 

        else: 

            if ind is None: 

                self._array[:self._len] += B * alpha 

            else: 

                self._array[ind] += B * alpha 

 

    def dot(self, other, pairwise, ind=None, o_ind=None): 

        assert self.check_ind(ind) 

        assert other.check_ind(o_ind) 

        assert self.dim == other.dim 

 

        if NUMPY_INDEX_QUIRK: 

            if self._len == 0 and hasattr(ind, '__len__'): 

                ind = None 

            if other._len == 0 and hasattr(o_ind, '__len__'): 

                o_ind = None 

 

        A = self._array[:self._len] if ind is None else \ 

            self._array[ind] if hasattr(ind, '__len__') else self._array[ind:ind + 1] 

        B = other._array[:other._len] if o_ind is None else \ 

            other._array[o_ind] if hasattr(o_ind, '__len__') else other._array[o_ind:o_ind + 1] 

 

        if pairwise: 

            assert self.len_ind(ind) == other.len_ind(o_ind) 

            return np.sum(A * B, axis=1) 

        else: 

            return A.dot(B.T) 

 

    def lincomb(self, coefficients, ind=None): 

        assert self.check_ind(ind) 

        assert 1 <= coefficients.ndim <= 2 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            ind = None 

 

        if coefficients.ndim == 1: 

            coefficients = coefficients[np.newaxis, ...] 

 

        assert ind is None and coefficients.shape[1] == len(self) \ 

            or not hasattr(ind, '__len__') and coefficients.shape[1] == 1 \ 

            or hasattr(ind, '__len__') and coefficients.shape[1] == len(ind) 

 

        if ind is None: 

            return NumpyVectorArray(coefficients.dot(self._array[:self._len]), copy=False) 

        elif hasattr(ind, '__len__'): 

            return NumpyVectorArray(coefficients.dot(self._array[ind]), copy=False) 

        else: 

            return NumpyVectorArray(coefficients.dot(self._array[ind:ind + 1]), copy=False) 

 

    def l1_norm(self, ind=None): 

        assert self.check_ind(ind) 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            ind = None 

 

        A = self._array[:self._len] if ind is None else \ 

            self._array[ind] if hasattr(ind, '__len__') else self._array[ind:ind + 1] 

 

        return np.sum(np.abs(A), axis=1) 

 

    def l2_norm(self, ind=None): 

        assert self.check_ind(ind) 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            ind = None 

 

        A = self._array[:self._len] if ind is None else \ 

            self._array[ind] if hasattr(ind, '__len__') else self._array[ind:ind + 1] 

 

        return np.sum(np.power(A, 2), axis=1)**(1/2) 

 

    def components(self, component_indices, ind=None): 

        assert self.check_ind(ind) 

        assert isinstance(component_indices, list) and (len(component_indices) == 0 or min(component_indices) >= 0) \ 

            or (isinstance(component_indices, np.ndarray) and component_indices.ndim == 1 

                and (len(component_indices) == 0 or np.min(component_indices) >= 0)) 

 

        if NUMPY_INDEX_QUIRK and (self._len == 0 or self.dim == 0): 

            assert isinstance(component_indices, list) \ 

                and (len(component_indices) == 0 or max(component_indices) < self.dim) \ 

                or isinstance(component_indices, np.ndarray) \ 

                and component_indices.ndim == 1 \ 

                and (len(component_indices) == 0 or np.max(component_indices) < self.dim) 

            return np.zeros((self.len_ind(ind), len(component_indices))) 

 

        if ind is None: 

            return self._array[:self._len, component_indices] 

        else: 

            if not hasattr(ind, '__len__'): 

                ind = [ind] 

            return self._array[:, component_indices][ind, :] 

 

    def amax(self, ind=None): 

        assert self.dim > 0 

        assert self.check_ind(ind) 

 

        if NUMPY_INDEX_QUIRK and self._len == 0: 

            ind = None 

 

        if self._array.shape[1] == 0: 

            l = self.len_ind(ind) 

            return (np.ones(l) * -1, np.zeros(l)) 

 

        A = self._array[:self._len] if ind is None else \ 

            self._array[ind] if hasattr(ind, '__len__') else self._array[ind:ind + 1] 

 

        A = np.abs(A) 

        max_ind = np.argmax(A, axis=1) 

        max_val = A[np.arange(len(A)), max_ind] 

        return (max_ind, max_val) 

 

    def __str__(self): 

        return self._array[:self._len].__str__() 

 

    def __repr__(self): 

        return 'NumpyVectorArray({})'.format(self._array[:self._len].__str__())