Simple matrix calculation in Scala -


suppose i've got matrix of integers a , need create new matrix b b[i, j] sum of a[i, j] , neighbors.

i define matrix array[array[int]] , write function foo calculate b this:

type matrix = array[array[int]]  def sumofneighbors(a: matrix, x: int, y: int): int = {    val range = -1 1   val deltas = range.flatmap { dx => range map {dy => (dx, dy)} }                     .filter  { case (dx, dy) => dx != 0 || dy != 0 }    val withinbounds: int => boolean = x => x >= 0 && x < a.size   val coords = deltas.map {case (dx, dy) => (x + dx, y + dy)}                      .filter {case (i, j) => withinbounds(i) && withinbounds(j)}    coords.map{case (i, j) => a(i)(j)}.sum }  def foo(a: matrix): matrix =   a.zipwithindex map {case (row, i) =>      row.zipwithindex.map {case (x, j) => x + sumofneighbors(a, i, j)}    } 

does make sense ? how simplify code ?

your code make sense, there couple tools add make expandable, depending on resources.
if combine spark scala code (if feasible) can utilize of mean-shift packages available spark. using rdds can lot of same functions.
https://spark-packages.org/?q=tags%3alsh

if small-scale though, none of necessary.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -