Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector to matrix (and similar operations) #113

Open
justinlovinger opened this issue Jan 22, 2021 · 1 comment
Open

Add vector to matrix (and similar operations) #113

justinlovinger opened this issue Jan 22, 2021 · 1 comment

Comments

@justinlovinger
Copy link

For example:

[ [ 0 1 2 ]
  [ 3 4 5 ] ]
+
  [ 0 1 2 ]
=
[ [ 0 2 4 ]
  [ 3 5 7 ] ]

I frequently need operations like this when writing machine learning code.

Related: #60.

@ukari
Copy link

ukari commented Jun 24, 2021

for reference:

broadcastPointwiseAdd :: (Numeric r e, Mutable r Ix2 e, Manifest r Ix1 e) => Array r Ix2 e -> Array r Ix1 e -> Array r Ix2 e
broadcastPointwiseAdd m v = m !+! (compute . Massiv.transpose . expandWithin Dim1 (fst . unconsSz . size $ m) const $ v)

test :: IO ()
test = do
  let w = compute $ resize' (Sz (2 :. 3)) (0 ... 5) :: Array U Ix2 Int
  let b = compute (0 ... 2) :: Array U Ix1 Int
  print w
  print b
  print $ w `broadcastPointwiseAdd` b
  pure ()
λ> test
Array U Seq (Sz (2 :. 3))
  [ [ 0, 1, 2 ]
  , [ 3, 4, 5 ]
  ]
Array U Seq (Sz1 3)
  [ 0, 1, 2 ]
Array U Seq (Sz (2 :. 3))
  [ [ 0, 2, 4 ]
  , [ 3, 5, 7 ]
  ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants