0

So i’m learning Scala and tried to create a vector class based on array and create an addition and substraction operators to add 2 vectors. This is what I did so far. Can anyone help to figure out how to make it so that when I add to vectors of different length, it adds “0” to the array with the shorter length so that it’s length is equal to the one with the bigger length? Like add (1, 2) to (1, 2, 3) should return (2, 4, 3)

class Wektor(private val s: Array[Double]){
        class LengthNotEqualException(msg:String) extends Exception(msg)
        def get(index: Int):Double= s(index)
        def +(that: Wektor):Wektor=
          if(this.s.length != that.s.length)
                  throw new LengthNotEqualException("Wektory roznej dlugosci")
          else
          {
            val temp= new Array[Double](this.s.length)
            var i:Int= 0
                for(i <- 0 until this.s.length)
            {
                temp(i)= this.s(i) + that.get(i)
            }
            new Wektor(temp)    // zwraca nowy Wektor będący sumą danych wektorów
          }
        def -(that: Wektor):Wektor=
          if(this.s.length != that.s.length)
                  throw new LengthNotEqualException("Wektory roznej dlugosci")
          else
          {
            val temp= new Array[Double](this.s.length)
            var i= 0
                for(i <- 0 until this.s.length)
            {
                temp(i)= this.s(i) - that.get(i)
            }
            new Wektor(temp)    // zwraca nowy Wektor będący różnicą danych wektorów
          }
        def *+(that:Wektor):Double=
          if(this.s.length != that.s.length)
                  throw new LengthNotEqualException("Wektory roznej dlugosci")
          else
                {
                  var result:Double= 0
                  var i:Int = 0
                  for(i <- 0 until this.s.length)
                  {
                result= result + this.s(i) * that.get(i)
                  }
                  result        // zwracany iloczyn skalarny
                }
        override def toString():String={
          var result:String="Wektor: ["
          var i:Int= 0
          for(i <- 0 until this.s.length)
          {
                  result= result + this.s(i) + " "
          }
          result = result + "]"
          result        // zwracana wartosc
        }
}

val test= new Wektor(Array[Double](1, 2, 3,5))
val test2= new Wektor(Array[Double](2, 2, 2))
val suma= test + test2
val roznica= test - test2
val iloczyn= test *+ test2
println(suma)
println(roznica)
println(iloczyn)

1 Answer 1

4

Use zipAll like this:

case class Wektor(inner: IndexedSeq[Double]) {
  def +(that: Wektor) =
    Wektor(this.inner.zipAll(that.inner, 0.0, 0.0).map{case (a, b) => a+b})
  def -(that: Wektor) =
    Wektor(this.inner.zipAll(that.inner, 0.0, 0.0).map{case (a, b) => a-b})
  def *+(that: Wektor) =
    this.inner.zipAll(that.inner, 1.0, 1.0).map{case (a, b) => a*b}.sum

  override def toString() = inner.mkString("Wektor: [", " ", "]")
}

val a = Wektor((1 to 5).map{_.toDouble})
// Wektor: [1.0 2.0 3.0 4.0 5.0]
val b = Wektor((1 to 3).map{_.toDouble})
// Wektor: [1.0 2.0 3.0]

a + b
// Wektor: [2.0 4.0 6.0 4.0 5.0]

a - b
// Wektor: [0.0 0.0 0.0 4.0 5.0]

a *+ b
// 23.0
Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I didn't even know such method existed. I even added it to my C# IEnumerable<T> extensions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.