vector.go 17 KB

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