How to resolve the algorithm Vector step by step in the Nim programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Vector step by step in the Nim programming language

Table of Contents

Problem Statement

Implement a Vector class (or a set of functions) that models a Physical Vector. The four basic operations and a pretty print function should be implemented.

The Vector may be initialized in any reasonable way.

The four operations to be implemented are:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vector step by step in the Nim programming language

Source code in the nim programming language

import strformat

type Vec2[T: SomeNumber] = tuple[x, y: T]

proc initVec2[T](x, y: T): Vec2[T] = (x, y)

func`+`[T](a, b: Vec2[T]): Vec2[T] = (a.x + b.x, a.y + b.y)

func `-`[T](a, b: Vec2[T]): Vec2[T] = (a.x - b.x, a.y - b.y)

func `*`[T](a: Vec2[T]; m: T): Vec2[T] = (a.x * m, a.y * m)

func `/`[T](a: Vec2[T]; d: T): Vec2[T] =
  if d == 0:
    raise newException(DivByZeroDefect, "division of vector by 0")
  when T is SomeInteger:
    (a.x div d, a.y div d)
  else:
    (a.x / d, a.y / d)

func `$`[T](a: Vec2[T]): string =
  &"({a.x}, {a.y})"

# Three ways to initialize a vector.
let v1 = initVec2(2, 3)
let v2: Vec2[int] = (-1, 2)
let v3 = (x: 4, y: -2)

echo &"{v1} + {v2} = {v1 + v2}"
echo &"{v3} - {v2} = {v3 - v2}"

# Float vectors.
let v4 = initVec2(2.0, 3.0)
let v5 = (x: 3.0, y: 2.0)

echo &"{v4} * 2 = {v4 * 2}"
echo &"{v3} / 2 = {v3 / 2}"   # Int division.
echo &"{v5} / 2 = {v5 / 2}"   # Float division.


  

You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the APL programming language
You may also check:How to resolve the algorithm Non-decimal radices/Input step by step in the Go programming language
You may also check:How to resolve the algorithm Currying step by step in the REXX programming language
You may also check:How to resolve the algorithm Knapsack problem/0-1 step by step in the OCaml programming language
You may also check:How to resolve the algorithm Rosetta Code/Find bare lang tags step by step in the Python programming language