vector.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // Copyright ©2013 The Gonum Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package mat
  5. import (
  6. "gonum.org/v1/gonum/blas"
  7. "gonum.org/v1/gonum/blas/blas64"
  8. "gonum.org/v1/gonum/internal/asm/f64"
  9. )
  10. var (
  11. vector *VecDense
  12. _ Matrix = vector
  13. _ allMatrix = vector
  14. _ Vector = vector
  15. _ Reseter = vector
  16. _ MutableVector = vector
  17. )
  18. // Vector is a vector.
  19. type Vector interface {
  20. Matrix
  21. AtVec(int) float64
  22. Len() int
  23. }
  24. // A MutableVector can set elements of a vector.
  25. type MutableVector interface {
  26. Vector
  27. SetVec(i int, v float64)
  28. }
  29. // TransposeVec is a type for performing an implicit transpose of a Vector.
  30. // It implements the Vector interface, returning values from the transpose
  31. // of the vector within.
  32. type TransposeVec struct {
  33. Vector Vector
  34. }
  35. // At returns the value of the element at row i and column j of the transposed
  36. // matrix, that is, row j and column i of the Vector field.
  37. func (t TransposeVec) At(i, j int) float64 {
  38. return t.Vector.At(j, i)
  39. }
  40. // AtVec returns the element at position i. It panics if i is out of bounds.
  41. func (t TransposeVec) AtVec(i int) float64 {
  42. return t.Vector.AtVec(i)
  43. }
  44. // Dims returns the dimensions of the transposed vector.
  45. func (t TransposeVec) Dims() (r, c int) {
  46. c, r = t.Vector.Dims()
  47. return r, c
  48. }
  49. // T performs an implicit transpose by returning the Vector field.
  50. func (t TransposeVec) T() Matrix {
  51. return t.Vector
  52. }
  53. // Len returns the number of columns in the vector.
  54. func (t TransposeVec) Len() int {
  55. return t.Vector.Len()
  56. }
  57. // TVec performs an implicit transpose by returning the Vector field.
  58. func (t TransposeVec) TVec() Vector {
  59. return t.Vector
  60. }
  61. // Untranspose returns the Vector field.
  62. func (t TransposeVec) Untranspose() Matrix {
  63. return t.Vector
  64. }
  65. func (t TransposeVec) UntransposeVec() Vector {
  66. return t.Vector
  67. }
  68. // VecDense represents a column vector.
  69. type VecDense struct {
  70. mat blas64.Vector
  71. // A BLAS vector can have a negative increment, but allowing this
  72. // in the mat type complicates a lot of code, and doesn't gain anything.
  73. // VecDense must have positive increment in this package.
  74. }
  75. // NewVecDense creates a new VecDense of length n. If data == nil,
  76. // a new slice is allocated for the backing slice. If len(data) == n, data is
  77. // used as the backing slice, and changes to the elements of the returned VecDense
  78. // will be reflected in data. If neither of these is true, NewVecDense will panic.
  79. // NewVecDense will panic if n is zero.
  80. func NewVecDense(n int, data []float64) *VecDense {
  81. if n <= 0 {
  82. if n == 0 {
  83. panic(ErrZeroLength)
  84. }
  85. panic("mat: negative dimension")
  86. }
  87. if len(data) != n && data != nil {
  88. panic(ErrShape)
  89. }
  90. if data == nil {
  91. data = make([]float64, n)
  92. }
  93. return &VecDense{
  94. mat: blas64.Vector{
  95. N: n,
  96. Inc: 1,
  97. Data: data,
  98. },
  99. }
  100. }
  101. // SliceVec returns a new Vector that shares backing data with the receiver.
  102. // The returned matrix starts at i of the receiver and extends k-i elements.
  103. // SliceVec panics with ErrIndexOutOfRange if the slice is outside the capacity
  104. // of the receiver.
  105. func (v *VecDense) SliceVec(i, k int) Vector {
  106. return v.sliceVec(i, k)
  107. }
  108. func (v *VecDense) sliceVec(i, k int) *VecDense {
  109. if i < 0 || k <= i || v.Cap() < k {
  110. panic(ErrIndexOutOfRange)
  111. }
  112. return &VecDense{
  113. mat: blas64.Vector{
  114. N: k - i,
  115. Inc: v.mat.Inc,
  116. Data: v.mat.Data[i*v.mat.Inc : (k-1)*v.mat.Inc+1],
  117. },
  118. }
  119. }
  120. // Dims returns the number of rows and columns in the matrix. Columns is always 1
  121. // for a non-Reset vector.
  122. func (v *VecDense) Dims() (r, c int) {
  123. if v.IsEmpty() {
  124. return 0, 0
  125. }
  126. return v.mat.N, 1
  127. }
  128. // Caps returns the number of rows and columns in the backing matrix. Columns is always 1
  129. // for a non-Reset vector.
  130. func (v *VecDense) Caps() (r, c int) {
  131. if v.IsEmpty() {
  132. return 0, 0
  133. }
  134. return v.Cap(), 1
  135. }
  136. // Len returns the length of the vector.
  137. func (v *VecDense) Len() int {
  138. return v.mat.N
  139. }
  140. // Cap returns the capacity of the vector.
  141. func (v *VecDense) Cap() int {
  142. if v.IsEmpty() {
  143. return 0
  144. }
  145. return (cap(v.mat.Data)-1)/v.mat.Inc + 1
  146. }
  147. // T performs an implicit transpose by returning the receiver inside a Transpose.
  148. func (v *VecDense) T() Matrix {
  149. return Transpose{v}
  150. }
  151. // TVec performs an implicit transpose by returning the receiver inside a TransposeVec.
  152. func (v *VecDense) TVec() Vector {
  153. return TransposeVec{v}
  154. }
  155. // Reset empties the matrix so that it can be reused as the
  156. // receiver of a dimensionally restricted operation.
  157. //
  158. // Reset should not be used when the matrix shares backing data.
  159. // See the Reseter interface for more information.
  160. func (v *VecDense) Reset() {
  161. // No change of Inc or N to 0 may be
  162. // made unless both are set to 0.
  163. v.mat.Inc = 0
  164. v.mat.N = 0
  165. v.mat.Data = v.mat.Data[:0]
  166. }
  167. // Zero sets all of the matrix elements to zero.
  168. func (v *VecDense) Zero() {
  169. for i := 0; i < v.mat.N; i++ {
  170. v.mat.Data[v.mat.Inc*i] = 0
  171. }
  172. }
  173. // CloneFromVec makes a copy of a into the receiver, overwriting the previous value
  174. // of the receiver.
  175. func (v *VecDense) CloneFromVec(a Vector) {
  176. if v == a {
  177. return
  178. }
  179. n := a.Len()
  180. v.mat = blas64.Vector{
  181. N: n,
  182. Inc: 1,
  183. Data: use(v.mat.Data, n),
  184. }
  185. if r, ok := a.(RawVectorer); ok {
  186. blas64.Copy(r.RawVector(), v.mat)
  187. return
  188. }
  189. for i := 0; i < a.Len(); i++ {
  190. v.setVec(i, a.AtVec(i))
  191. }
  192. }
  193. // VecDenseCopyOf returns a newly allocated copy of the elements of a.
  194. func VecDenseCopyOf(a Vector) *VecDense {
  195. v := &VecDense{}
  196. v.CloneFromVec(a)
  197. return v
  198. }
  199. // RawVector returns the underlying blas64.Vector used by the receiver.
  200. // Changes to elements in the receiver following the call will be reflected
  201. // in returned blas64.Vector.
  202. func (v *VecDense) RawVector() blas64.Vector {
  203. return v.mat
  204. }
  205. // SetRawVector sets the underlying blas64.Vector used by the receiver.
  206. // Changes to elements in the receiver following the call will be reflected
  207. // in the input.
  208. func (v *VecDense) SetRawVector(a blas64.Vector) {
  209. v.mat = a
  210. }
  211. // CopyVec makes a copy of elements of a into the receiver. It is similar to the
  212. // built-in copy; it copies as much as the overlap between the two vectors and
  213. // returns the number of elements it copied.
  214. func (v *VecDense) CopyVec(a Vector) int {
  215. n := min(v.Len(), a.Len())
  216. if v == a {
  217. return n
  218. }
  219. if r, ok := a.(RawVectorer); ok {
  220. src := r.RawVector()
  221. src.N = n
  222. dst := v.mat
  223. dst.N = n
  224. blas64.Copy(src, dst)
  225. return n
  226. }
  227. for i := 0; i < n; i++ {
  228. v.setVec(i, a.AtVec(i))
  229. }
  230. return n
  231. }
  232. // ScaleVec scales the vector a by alpha, placing the result in the receiver.
  233. func (v *VecDense) ScaleVec(alpha float64, a Vector) {
  234. n := a.Len()
  235. if v == a {
  236. if v.mat.Inc == 1 {
  237. f64.ScalUnitary(alpha, v.mat.Data)
  238. return
  239. }
  240. f64.ScalInc(alpha, v.mat.Data, uintptr(n), uintptr(v.mat.Inc))
  241. return
  242. }
  243. v.reuseAsNonZeroed(n)
  244. if rv, ok := a.(RawVectorer); ok {
  245. mat := rv.RawVector()
  246. v.checkOverlap(mat)
  247. if v.mat.Inc == 1 && mat.Inc == 1 {
  248. f64.ScalUnitaryTo(v.mat.Data, alpha, mat.Data)
  249. return
  250. }
  251. f64.ScalIncTo(v.mat.Data, uintptr(v.mat.Inc),
  252. alpha, mat.Data, uintptr(n), uintptr(mat.Inc))
  253. return
  254. }
  255. for i := 0; i < n; i++ {
  256. v.setVec(i, alpha*a.AtVec(i))
  257. }
  258. }
  259. // AddScaledVec adds the vectors a and alpha*b, placing the result in the receiver.
  260. func (v *VecDense) AddScaledVec(a Vector, alpha float64, b Vector) {
  261. if alpha == 1 {
  262. v.AddVec(a, b)
  263. return
  264. }
  265. if alpha == -1 {
  266. v.SubVec(a, b)
  267. return
  268. }
  269. ar := a.Len()
  270. br := b.Len()
  271. if ar != br {
  272. panic(ErrShape)
  273. }
  274. var amat, bmat blas64.Vector
  275. fast := true
  276. aU, _ := untransposeExtract(a)
  277. if rv, ok := aU.(*VecDense); ok {
  278. amat = rv.mat
  279. if v != a {
  280. v.checkOverlap(amat)
  281. }
  282. } else {
  283. fast = false
  284. }
  285. bU, _ := untransposeExtract(b)
  286. if rv, ok := bU.(*VecDense); ok {
  287. bmat = rv.mat
  288. if v != b {
  289. v.checkOverlap(bmat)
  290. }
  291. } else {
  292. fast = false
  293. }
  294. v.reuseAsNonZeroed(ar)
  295. switch {
  296. case alpha == 0: // v <- a
  297. if v == a {
  298. return
  299. }
  300. v.CopyVec(a)
  301. case v == a && v == b: // v <- v + alpha * v = (alpha + 1) * v
  302. blas64.Scal(alpha+1, v.mat)
  303. case !fast: // v <- a + alpha * b without blas64 support.
  304. for i := 0; i < ar; i++ {
  305. v.setVec(i, a.AtVec(i)+alpha*b.AtVec(i))
  306. }
  307. case v == a && v != b: // v <- v + alpha * b
  308. if v.mat.Inc == 1 && bmat.Inc == 1 {
  309. // Fast path for a common case.
  310. f64.AxpyUnitaryTo(v.mat.Data, alpha, bmat.Data, amat.Data)
  311. } else {
  312. f64.AxpyInc(alpha, bmat.Data, v.mat.Data,
  313. uintptr(ar), uintptr(bmat.Inc), uintptr(v.mat.Inc), 0, 0)
  314. }
  315. default: // v <- a + alpha * b or v <- a + alpha * v
  316. if v.mat.Inc == 1 && amat.Inc == 1 && bmat.Inc == 1 {
  317. // Fast path for a common case.
  318. f64.AxpyUnitaryTo(v.mat.Data, alpha, bmat.Data, amat.Data)
  319. } else {
  320. f64.AxpyIncTo(v.mat.Data, uintptr(v.mat.Inc), 0,
  321. alpha, bmat.Data, amat.Data,
  322. uintptr(ar), uintptr(bmat.Inc), uintptr(amat.Inc), 0, 0)
  323. }
  324. }
  325. }
  326. // AddVec adds the vectors a and b, placing the result in the receiver.
  327. func (v *VecDense) AddVec(a, b Vector) {
  328. ar := a.Len()
  329. br := b.Len()
  330. if ar != br {
  331. panic(ErrShape)
  332. }
  333. v.reuseAsNonZeroed(ar)
  334. aU, _ := untransposeExtract(a)
  335. bU, _ := untransposeExtract(b)
  336. if arv, ok := aU.(*VecDense); ok {
  337. if brv, ok := bU.(*VecDense); ok {
  338. amat := arv.mat
  339. bmat := brv.mat
  340. if v != a {
  341. v.checkOverlap(amat)
  342. }
  343. if v != b {
  344. v.checkOverlap(bmat)
  345. }
  346. if v.mat.Inc == 1 && amat.Inc == 1 && bmat.Inc == 1 {
  347. // Fast path for a common case.
  348. f64.AxpyUnitaryTo(v.mat.Data, 1, bmat.Data, amat.Data)
  349. return
  350. }
  351. f64.AxpyIncTo(v.mat.Data, uintptr(v.mat.Inc), 0,
  352. 1, bmat.Data, amat.Data,
  353. uintptr(ar), uintptr(bmat.Inc), uintptr(amat.Inc), 0, 0)
  354. return
  355. }
  356. }
  357. for i := 0; i < ar; i++ {
  358. v.setVec(i, a.AtVec(i)+b.AtVec(i))
  359. }
  360. }
  361. // SubVec subtracts the vector b from a, placing the result in the receiver.
  362. func (v *VecDense) SubVec(a, b Vector) {
  363. ar := a.Len()
  364. br := b.Len()
  365. if ar != br {
  366. panic(ErrShape)
  367. }
  368. v.reuseAsNonZeroed(ar)
  369. aU, _ := untransposeExtract(a)
  370. bU, _ := untransposeExtract(b)
  371. if arv, ok := aU.(*VecDense); ok {
  372. if brv, ok := bU.(*VecDense); ok {
  373. amat := arv.mat
  374. bmat := brv.mat
  375. if v != a {
  376. v.checkOverlap(amat)
  377. }
  378. if v != b {
  379. v.checkOverlap(bmat)
  380. }
  381. if v.mat.Inc == 1 && amat.Inc == 1 && bmat.Inc == 1 {
  382. // Fast path for a common case.
  383. f64.AxpyUnitaryTo(v.mat.Data, -1, bmat.Data, amat.Data)
  384. return
  385. }
  386. f64.AxpyIncTo(v.mat.Data, uintptr(v.mat.Inc), 0,
  387. -1, bmat.Data, amat.Data,
  388. uintptr(ar), uintptr(bmat.Inc), uintptr(amat.Inc), 0, 0)
  389. return
  390. }
  391. }
  392. for i := 0; i < ar; i++ {
  393. v.setVec(i, a.AtVec(i)-b.AtVec(i))
  394. }
  395. }
  396. // MulElemVec performs element-wise multiplication of a and b, placing the result
  397. // in the receiver.
  398. func (v *VecDense) MulElemVec(a, b Vector) {
  399. ar := a.Len()
  400. br := b.Len()
  401. if ar != br {
  402. panic(ErrShape)
  403. }
  404. v.reuseAsNonZeroed(ar)
  405. aU, _ := untransposeExtract(a)
  406. bU, _ := untransposeExtract(b)
  407. if arv, ok := aU.(*VecDense); ok {
  408. if brv, ok := bU.(*VecDense); ok {
  409. amat := arv.mat
  410. bmat := brv.mat
  411. if v != a {
  412. v.checkOverlap(amat)
  413. }
  414. if v != b {
  415. v.checkOverlap(bmat)
  416. }
  417. if v.mat.Inc == 1 && amat.Inc == 1 && bmat.Inc == 1 {
  418. // Fast path for a common case.
  419. for i, a := range amat.Data {
  420. v.mat.Data[i] = a * bmat.Data[i]
  421. }
  422. return
  423. }
  424. var ia, ib int
  425. for i := 0; i < ar; i++ {
  426. v.setVec(i, amat.Data[ia]*bmat.Data[ib])
  427. ia += amat.Inc
  428. ib += bmat.Inc
  429. }
  430. return
  431. }
  432. }
  433. for i := 0; i < ar; i++ {
  434. v.setVec(i, a.AtVec(i)*b.AtVec(i))
  435. }
  436. }
  437. // DivElemVec performs element-wise division of a by b, placing the result
  438. // in the receiver.
  439. func (v *VecDense) DivElemVec(a, b Vector) {
  440. ar := a.Len()
  441. br := b.Len()
  442. if ar != br {
  443. panic(ErrShape)
  444. }
  445. v.reuseAsNonZeroed(ar)
  446. aU, _ := untransposeExtract(a)
  447. bU, _ := untransposeExtract(b)
  448. if arv, ok := aU.(*VecDense); ok {
  449. if brv, ok := bU.(*VecDense); ok {
  450. amat := arv.mat
  451. bmat := brv.mat
  452. if v != a {
  453. v.checkOverlap(amat)
  454. }
  455. if v != b {
  456. v.checkOverlap(bmat)
  457. }
  458. if v.mat.Inc == 1 && amat.Inc == 1 && bmat.Inc == 1 {
  459. // Fast path for a common case.
  460. for i, a := range amat.Data {
  461. v.setVec(i, a/bmat.Data[i])
  462. }
  463. return
  464. }
  465. var ia, ib int
  466. for i := 0; i < ar; i++ {
  467. v.setVec(i, amat.Data[ia]/bmat.Data[ib])
  468. ia += amat.Inc
  469. ib += bmat.Inc
  470. }
  471. }
  472. }
  473. for i := 0; i < ar; i++ {
  474. v.setVec(i, a.AtVec(i)/b.AtVec(i))
  475. }
  476. }
  477. // MulVec computes a * b. The result is stored into the receiver.
  478. // MulVec panics if the number of columns in a does not equal the number of rows in b
  479. // or if the number of columns in b does not equal 1.
  480. func (v *VecDense) MulVec(a Matrix, b Vector) {
  481. r, c := a.Dims()
  482. br, bc := b.Dims()
  483. if c != br || bc != 1 {
  484. panic(ErrShape)
  485. }
  486. aU, trans := untransposeExtract(a)
  487. var bmat blas64.Vector
  488. fast := true
  489. bU, _ := untransposeExtract(b)
  490. if rv, ok := bU.(*VecDense); ok {
  491. bmat = rv.mat
  492. if v != b {
  493. v.checkOverlap(bmat)
  494. }
  495. } else {
  496. fast = false
  497. }
  498. v.reuseAsNonZeroed(r)
  499. var restore func()
  500. if v == aU {
  501. v, restore = v.isolatedWorkspace(aU.(*VecDense))
  502. defer restore()
  503. } else if v == b {
  504. v, restore = v.isolatedWorkspace(b)
  505. defer restore()
  506. }
  507. // TODO(kortschak): Improve the non-fast paths.
  508. switch aU := aU.(type) {
  509. case Vector:
  510. if b.Len() == 1 {
  511. // {n,1} x {1,1}
  512. v.ScaleVec(b.AtVec(0), aU)
  513. return
  514. }
  515. // {1,n} x {n,1}
  516. if fast {
  517. if rv, ok := aU.(*VecDense); ok {
  518. amat := rv.mat
  519. if v != aU {
  520. v.checkOverlap(amat)
  521. }
  522. if amat.Inc == 1 && bmat.Inc == 1 {
  523. // Fast path for a common case.
  524. v.setVec(0, f64.DotUnitary(amat.Data, bmat.Data))
  525. return
  526. }
  527. v.setVec(0, f64.DotInc(amat.Data, bmat.Data,
  528. uintptr(c), uintptr(amat.Inc), uintptr(bmat.Inc), 0, 0))
  529. return
  530. }
  531. }
  532. var sum float64
  533. for i := 0; i < c; i++ {
  534. sum += aU.AtVec(i) * b.AtVec(i)
  535. }
  536. v.setVec(0, sum)
  537. return
  538. case *SymBandDense:
  539. if fast {
  540. aU.checkOverlap(v.asGeneral())
  541. blas64.Sbmv(1, aU.mat, bmat, 0, v.mat)
  542. return
  543. }
  544. case *SymDense:
  545. if fast {
  546. aU.checkOverlap(v.asGeneral())
  547. blas64.Symv(1, aU.mat, bmat, 0, v.mat)
  548. return
  549. }
  550. case *TriDense:
  551. v.CopyVec(b)
  552. aU.checkOverlap(v.asGeneral())
  553. ta := blas.NoTrans
  554. if trans {
  555. ta = blas.Trans
  556. }
  557. blas64.Trmv(ta, aU.mat, v.mat)
  558. case *Dense:
  559. if fast {
  560. aU.checkOverlap(v.asGeneral())
  561. t := blas.NoTrans
  562. if trans {
  563. t = blas.Trans
  564. }
  565. blas64.Gemv(t, 1, aU.mat, bmat, 0, v.mat)
  566. return
  567. }
  568. default:
  569. if fast {
  570. for i := 0; i < r; i++ {
  571. var f float64
  572. for j := 0; j < c; j++ {
  573. f += a.At(i, j) * bmat.Data[j*bmat.Inc]
  574. }
  575. v.setVec(i, f)
  576. }
  577. return
  578. }
  579. }
  580. for i := 0; i < r; i++ {
  581. var f float64
  582. for j := 0; j < c; j++ {
  583. f += a.At(i, j) * b.AtVec(j)
  584. }
  585. v.setVec(i, f)
  586. }
  587. }
  588. // ReuseAsVec changes the receiver if it IsEmpty() to be of size n×1.
  589. //
  590. // ReuseAsVec re-uses the backing data slice if it has sufficient capacity,
  591. // otherwise a new slice is allocated. The backing data is zero on return.
  592. //
  593. // ReuseAsVec panics if the receiver is not empty, and panics if
  594. // the input size is less than one. To empty the receiver for re-use,
  595. // Reset should be used.
  596. func (v *VecDense) ReuseAsVec(n int) {
  597. if n <= 0 {
  598. if n == 0 {
  599. panic(ErrZeroLength)
  600. }
  601. panic(ErrNegativeDimension)
  602. }
  603. if !v.IsEmpty() {
  604. panic(ErrReuseNonEmpty)
  605. }
  606. v.reuseAsZeroed(n)
  607. }
  608. // reuseAsNonZeroed resizes an empty vector to a r×1 vector,
  609. // or checks that a non-empty matrix is r×1.
  610. func (v *VecDense) reuseAsNonZeroed(r int) {
  611. // reuseAsNonZeroed must be kept in sync with reuseAsZeroed.
  612. if r == 0 {
  613. panic(ErrZeroLength)
  614. }
  615. if v.IsEmpty() {
  616. v.mat = blas64.Vector{
  617. N: r,
  618. Inc: 1,
  619. Data: use(v.mat.Data, r),
  620. }
  621. return
  622. }
  623. if r != v.mat.N {
  624. panic(ErrShape)
  625. }
  626. }
  627. // reuseAsZeroed resizes an empty vector to a r×1 vector,
  628. // or checks that a non-empty matrix is r×1.
  629. func (v *VecDense) reuseAsZeroed(r int) {
  630. // reuseAsZeroed must be kept in sync with reuseAsNonZeroed.
  631. if r == 0 {
  632. panic(ErrZeroLength)
  633. }
  634. if v.IsEmpty() {
  635. v.mat = blas64.Vector{
  636. N: r,
  637. Inc: 1,
  638. Data: useZeroed(v.mat.Data, r),
  639. }
  640. return
  641. }
  642. if r != v.mat.N {
  643. panic(ErrShape)
  644. }
  645. v.Zero()
  646. }
  647. // IsEmpty returns whether the receiver is empty. Empty matrices can be the
  648. // receiver for size-restricted operations. The receiver can be emptied using
  649. // Reset.
  650. func (v *VecDense) IsEmpty() bool {
  651. // It must be the case that v.Dims() returns
  652. // zeros in this case. See comment in Reset().
  653. return v.mat.Inc == 0
  654. }
  655. func (v *VecDense) isolatedWorkspace(a Vector) (n *VecDense, restore func()) {
  656. l := a.Len()
  657. if l == 0 {
  658. panic(ErrZeroLength)
  659. }
  660. n = getWorkspaceVec(l, false)
  661. return n, func() {
  662. v.CopyVec(n)
  663. putWorkspaceVec(n)
  664. }
  665. }
  666. // asDense returns a Dense representation of the receiver with the same
  667. // underlying data.
  668. func (v *VecDense) asDense() *Dense {
  669. return &Dense{
  670. mat: v.asGeneral(),
  671. capRows: v.mat.N,
  672. capCols: 1,
  673. }
  674. }
  675. // asGeneral returns a blas64.General representation of the receiver with the
  676. // same underlying data.
  677. func (v *VecDense) asGeneral() blas64.General {
  678. return blas64.General{
  679. Rows: v.mat.N,
  680. Cols: 1,
  681. Stride: v.mat.Inc,
  682. Data: v.mat.Data,
  683. }
  684. }
  685. // ColViewOf reflects the column j of the RawMatrixer m, into the receiver
  686. // backed by the same underlying data. The receiver must either be empty
  687. // have length equal to the number of rows of m.
  688. func (v *VecDense) ColViewOf(m RawMatrixer, j int) {
  689. rm := m.RawMatrix()
  690. if j >= rm.Cols || j < 0 {
  691. panic(ErrColAccess)
  692. }
  693. if !v.IsEmpty() && v.mat.N != rm.Rows {
  694. panic(ErrShape)
  695. }
  696. v.mat.Inc = rm.Stride
  697. v.mat.Data = rm.Data[j : (rm.Rows-1)*rm.Stride+j+1]
  698. v.mat.N = rm.Rows
  699. }
  700. // RowViewOf reflects the row i of the RawMatrixer m, into the receiver
  701. // backed by the same underlying data. The receiver must either be
  702. // empty or have length equal to the number of columns of m.
  703. func (v *VecDense) RowViewOf(m RawMatrixer, i int) {
  704. rm := m.RawMatrix()
  705. if i >= rm.Rows || i < 0 {
  706. panic(ErrRowAccess)
  707. }
  708. if !v.IsEmpty() && v.mat.N != rm.Cols {
  709. panic(ErrShape)
  710. }
  711. v.mat.Inc = 1
  712. v.mat.Data = rm.Data[i*rm.Stride : i*rm.Stride+rm.Cols]
  713. v.mat.N = rm.Cols
  714. }