WeixiangYan/CodeTransOcean huggingface dataset:
Task: Horner's rule for polynomial evaluation


C++: #include <iostream>
#include <vector>

using namespace std;

double horner(vector<double> v, double x)
{
  double s = 0;
  
  for( vector<double>::const_reverse_iterator i = v.rbegin(); i != v.rend(); i++ )
    s = s*x + *i;
  return s;
}

int main()
{
  double c[] = { -19, 7, -4, 6 };
  vector<double> v(c, c + sizeof(c)/sizeof(double));
  cout << horner(v, 3.0) << endl;
  return 0;
}










Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Horner's rule for polynomial evaluation




Go: package main

import "fmt"

func horner(x int64, c []int64) (acc int64) {
    for i := len(c) - 1; i >= 0; i-- {
        acc = acc*x + c[i]
    }
    return
}

func main() {
    fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
}








Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Horner's rule for polynomial evaluation





Java: import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Horner {
    public static void main(String[] args){
        List<Double> coeffs = new ArrayList<Double>();
        coeffs.add(-19.0);
        coeffs.add(7.0);
        coeffs.add(-4.0);
        coeffs.add(6.0);
        System.out.println(polyEval(coeffs, 3));
    }

    public static double polyEval(List<Double> coefficients, double x) {
        Collections.reverse(coefficients);
        Double accumulator = coefficients.get(0);
        for (int i = 1; i < coefficients.size(); i++) {
            accumulator = (accumulator * x) + (Double) coefficients.get(i);
        }
        return accumulator;
    }
}







Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Horner's rule for polynomial evaluation







Python: >>> def horner(coeffs, x):
	acc = 0
	for c in reversed(coeffs):
		acc = acc * x + c
	return acc

>>> horner( (-19, 7, -4, 6), 3)
128





Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Horner's rule for polynomial evaluation








VB: Function horners_rule(coefficients,x)
	accumulator = 0
	For i = UBound(coefficients) To 0 Step -1
		accumulator = (accumulator * x) + coefficients(i)
	Next
	horners_rule = accumulator
End Function

WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)




Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Horner's rule for polynomial evaluation

C: #include <stdio.h>

double horner(double *coeffs, int s, double x)
{
  int i;
  double res = 0.0;
  
  for(i=s-1; i >= 0; i--)
  {
    res = res * x + coeffs[i];
  }
  return res;
}


int main()
{
  double coeffs[] = { -19.0, 7.0, -4.0, 6.0 };
  
  printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
  return 0;
}
Task: Horner's rule for polynomial evaluation



C#: using System;
using System.Linq;

class Program
{
    static double Horner(double[] coefficients, double variable)
    {
        return coefficients.Reverse().Aggregate(
                (accumulator, coefficient) => accumulator * variable + coefficient);
    }

    static void Main()
    {
        Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
    }
}
Task: Horner's rule for polynomial evaluation


C++: #include <iostream>
#include <vector>

using namespace std;

double horner(vector<double> v, double x)
{
  double s = 0;
  
  for( vector<double>::const_reverse_iterator i = v.rbegin(); i != v.rend(); i++ )
    s = s*x + *i;
  return s;
}

int main()
{
  double c[] = { -19, 7, -4, 6 };
  vector<double> v(c, c + sizeof(c)/sizeof(double));
  cout << horner(v, 3.0) << endl;
  return 0;
}
Task: Horner's rule for polynomial evaluation





Java: import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Horner {
    public static void main(String[] args){
        List<Double> coeffs = new ArrayList<Double>();
        coeffs.add(-19.0);
        coeffs.add(7.0);
        coeffs.add(-4.0);
        coeffs.add(6.0);
        System.out.println(polyEval(coeffs, 3));
    }

    public static double polyEval(List<Double> coefficients, double x) {
        Collections.reverse(coefficients);
        Double accumulator = coefficients.get(0);
        for (int i = 1; i < coefficients.size(); i++) {
            accumulator = (accumulator * x) + (Double) coefficients.get(i);
        }
        return accumulator;
    }
}
Task: Horner's rule for polynomial evaluation







Python: >>> def horner(coeffs, x):
	acc = 0
	for c in reversed(coeffs):
		acc = acc * x + c
	return acc

>>> horner( (-19, 7, -4, 6), 3)
128
Task: Horner's rule for polynomial evaluation








VB: Function horners_rule(coefficients,x)
	accumulator = 0
	For i = UBound(coefficients) To 0 Step -1
		accumulator = (accumulator * x) + coefficients(i)
	Next
	horners_rule = accumulator
End Function

WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)
Task: Horner's rule for polynomial evaluation




Go: package main

import "fmt"

func horner(x int64, c []int64) (acc int64) {
    for i := len(c) - 1; i >= 0; i-- {
        acc = acc*x + c[i]
    }
    return
}

func main() {
    fmt.Println(horner(3, []int64{-19, 7, -4, 6}))
}
Task: Horner's rule for polynomial evaluation



C#: using System;
using System.Linq;

class Program
{
    static double Horner(double[] coefficients, double variable)
    {
        return coefficients.Reverse().Aggregate(
                (accumulator, coefficient) => accumulator * variable + coefficient);
    }

    static void Main()
    {
        Console.WriteLine(Horner(new[] { -19.0, 7.0, -4.0, 6.0 }, 3.0));
    }
}








Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation


C++: #include <iostream>
#include <vector>

using namespace std;

double horner(vector<double> v, double x)
{
  double s = 0;
  
  for( vector<double>::const_reverse_iterator i = v.rbegin(); i != v.rend(); i++ )
    s = s*x + *i;
  return s;
}

int main()
{
  double c[] = { -19, 7, -4, 6 };
  vector<double> v(c, c + sizeof(c)/sizeof(double));
  cout << horner(v, 3.0) << endl;
  return 0;
}









Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation

C: #include <stdio.h>

double horner(double *coeffs, int s, double x)
{
  int i;
  double res = 0.0;
  
  for(i=s-1; i >= 0; i--)
  {
    res = res * x + coeffs[i];
  }
  return res;
}


int main()
{
  double coeffs[] = { -19.0, 7.0, -4.0, 6.0 };
  
  printf("%5.1f\n", horner(coeffs, sizeof(coeffs)/sizeof(double), 3.0));
  return 0;
}










Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation





Java: import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Horner {
    public static void main(String[] args){
        List<Double> coeffs = new ArrayList<Double>();
        coeffs.add(-19.0);
        coeffs.add(7.0);
        coeffs.add(-4.0);
        coeffs.add(6.0);
        System.out.println(polyEval(coeffs, 3));
    }

    public static double polyEval(List<Double> coefficients, double x) {
        Collections.reverse(coefficients);
        Double accumulator = coefficients.get(0);
        for (int i = 1; i < coefficients.size(); i++) {
            accumulator = (accumulator * x) + (Double) coefficients.get(i);
        }
        return accumulator;
    }
}






Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation







Python: >>> def horner(coeffs, x):
	acc = 0
	for c in reversed(coeffs):
		acc = acc * x + c
	return acc

>>> horner( (-19, 7, -4, 6), 3)
128




Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation








VB: Function horners_rule(coefficients,x)
	accumulator = 0
	For i = UBound(coefficients) To 0 Step -1
		accumulator = (accumulator * x) + coefficients(i)
	Next
	horners_rule = accumulator
End Function

WScript.StdOut.WriteLine horners_rule(Array(-19,7,-4,6),3)



Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation






PHP: <?php
function horner($coeff, $x) {
    $result = 0;
    foreach (array_reverse($coeff) as $c)
        $result = $result * $x + $c;
    return $result;
}
 
$coeff = array(-19.0, 7, -4, 6);
$x = 3;
echo horner($coeff, $x), "\n";
?>





Fortran: program test_horner

  implicit none

  write (*, '(f5.1)') horner ([-19.0, 7.0, -4.0, 6.0], 3.0)

contains

  function horner (coeffs, x) result (res)

    implicit none
    real, dimension (0:), intent (in) :: coeffs
    real, intent (in) :: x
    real :: res
    integer :: i

    res = coeffs(ubound(coeffs,1))
    do i = ubound(coeffs,1)-1, 0, -1
      res = res * x + coeffs (i)
    end do

  end function horner

end program test_horner
Task: Horner's rule for polynomial evaluation








VB: x = 3
dim coeficientes = {-19, 7, -4, 6}
print "Horner
print "6*x^3 - 4*x^2 + 7*x - 19 when x = 3: ";
print AlgoritmoHorner(coeficientes, x)
end

function AlgoritmoHorner(coeffs, x)
	acumulador = 0
	for i = coeffs[?]-1 to 0 step -1
		acumulador = (acumulador * x) + coeffs[i]
	next i
	return acumulador
end function
Task: Horner's rule for polynomial evaluation






PHP: <?php
function horner($coeff, $x) {
    $result = 0;
    foreach (array_reverse($coeff) as $c)
        $result = $result * $x + $c;
    return $result;
}
 
$coeff = array(-19.0, 7, -4, 6);
$x = 3;
echo horner($coeff, $x), "\n";
?>
Task: Horner's rule for polynomial evaluation






PHP: <?php
function horner($coeff, $x) {
    $result = 0;
    foreach (array_reverse($coeff) as $c)
        $result = $result * $x + $c;
    return $result;
}
 
$coeff = array(-19.0, 7, -4, 6);
$x = 3;
echo horner($coeff, $x), "\n";
?>






Ada: with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure Horners_Rule is
   type Coef is array(Positive range <>) of Float;

   function Horner(Coeffs: Coef; Val: Float) return Float is
      Res : Float := 0.0;
   begin
      for P in reverse Coeffs'Range loop
         Res := Res*Val + Coeffs(P);
      end loop;
      return Res;
   end Horner;

begin
   Put(Horner(Coeffs => (-19.0, 7.0, -4.0, 6.0), Val => 3.0), Aft=>1, Exp=>0);
end Horners_Rule;
Task: Matrix multiplication



C#: public class Matrix
{
	int n;
	int m;
	double[,] a;

	public Matrix(int n, int m)
	{
		if (n <= 0 || m <= 0)
			throw new ArgumentException("Matrix dimensions must be positive");
		this.n = n;
		this.m = m;
		a = new double[n, m];
	}

	
	public double this[int i, int j]
	{
		get { return a[i - 1, j - 1]; }
		set { a[i - 1, j - 1] = value; }
	}

	public int N { get { return n; } }
	public int M { get { return m; } }

	public static Matrix operator*(Matrix _a, Matrix b)
	{
		int n = _a.N;
		int m = b.M;
		int l = _a.M;
		if (l != b.N)
			throw new ArgumentException("Illegal matrix dimensions for multiplication. _a.M must be equal b.N");
		Matrix result = new Matrix(_a.N, b.M);
		for(int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
			{
				double sum = 0.0;
				for (int k = 0; k < l; k++)
					sum += _a.a[i, k]*b.a[k, j];
				result.a[i, j] = sum;
			}
		return result;
	}
}









Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;
Task: Matrix multiplication

C: #include <stdio.h>

#define MAT_ELEM(rows,cols,r,c) (r*cols+c)



#ifdef __cplusplus
    typedef double * const __restrict MAT_OUT_t;
#else
    typedef double * const restrict MAT_OUT_t;
#endif
typedef const double * const MAT_IN_t;

static inline void mat_mult(
    const int m,
    const int n,
    const int p, 
    MAT_IN_t a,
    MAT_IN_t b,
    MAT_OUT_t c)
{
    for (int row=0; row<m; row++) {
        for (int col=0; col<p; col++) {
            c[MAT_ELEM(m,p,row,col)] = 0;
            for (int i=0; i<n; i++) {
                c[MAT_ELEM(m,p,row,col)] += a[MAT_ELEM(m,n,row,i)]*b[MAT_ELEM(n,p,i,col)];
            }
        }
    }
}

static inline void mat_show(
    const int m,
    const int p,
    MAT_IN_t a)
{
    for (int row=0; row<m;row++) {
        for (int col=0; col<p;col++) {
            printf("\t%7.3f", a[MAT_ELEM(m,p,row,col)]);
        }
        putchar('\n');
    }
}

int main(void)
{
    double a[4*4] = {1, 1,   1,   1,
                     2, 4,   8,  16,
                     3, 9,  27,  81,
                     4, 16, 64, 256};

    double b[4*3] = {    4.0,   -3.0,  4.0/3,
                     -13.0/3, 19.0/4, -7.0/3,
                       3.0/2,   -2.0,  7.0/6,
                      -1.0/6,  1.0/4, -1.0/6};

    double c[4*3] = {0};
    
    mat_mult(4,4,3,a,b,c);
    mat_show(4,3,c);
    return 0;
}











Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;
Task: Matrix multiplication


C++: #include <iostream>
#include <blitz/tinymat.h>

int main()
{
  using namespace blitz;

  TinyMatrix<double,3,3> A, B, C;

  A = 1, 2, 3,
      4, 5, 6,
      7, 8, 9;

  B = 1, 0, 0,
      0, 1, 0,
      0, 0, 1;

  C = product(A, B);

  std::cout << C << std::endl;
}










Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;
Task: Matrix multiplication




Go: package main

import (
    "fmt"

    "gonum.org/v1/gonum/mat"
)

func main() {
    a := mat.NewDense(2, 4, []float64{
        1, 2, 3, 4,
        5, 6, 7, 8,
    })
    b := mat.NewDense(4, 3, []float64{
        1, 2, 3,
        4, 5, 6,
        7, 8, 9,
        10, 11, 12,
    })
    var m mat.Dense
    m.Mul(a, b)
    fmt.Println(mat.Formatted(&m))
}








Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;
Task: Matrix multiplication





Java: public static double[][] mult(double a[][], double b[][]){
   if(a.length == 0) return new double[0][0];
   if(a[0].length != b.length) return null; 

   int n = a[0].length;
   int m = a.length;
   int p = b[0].length;

   double ans[][] = new double[m][p];

   for(int i = 0;i < m;i++){
      for(int j = 0;j < p;j++){
         for(int k = 0;k < n;k++){
            ans[i][j] += a[i][k] * b[k][j];
         }
      }
   }
   return ans;
}







Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;
Task: Matrix multiplication







Python: a=((1,  1,  1,   1), 
     (2,  4,  8,  16),
     (3,  9, 27,  81),
     (4, 16, 64, 256))

b=((  4  , -3  ,  4/3.,  -1/4. ), 
     (-13/3., 19/4., -7/3.,  11/24.),
     (  3/2., -2.  ,  7/6.,  -1/4. ),
     ( -1/6.,  1/4., -1/6.,   1/24.))



def MatrixMul( mtx_a, mtx_b):
    tpos_b = zip( *mtx_b)
    rtn = [[ sum( ea*eb for ea,eb in zip(a,b)) for b in tpos_b] for a in mtx_a]
    return rtn


v = MatrixMul( a, b )

print 'v = ('
for r in v:
    print '[', 
    for val in r:
        print '%8.2f '%val, 
    print ']'
print ')'


u = MatrixMul(b,a)

print 'u = '
for r in u:
    print '[', 
    for val in r:
        print '%8.2f '%val, 
    print ']'
print ')'





Ada: with Ada.Text_IO;               use Ada.Text_IO;
with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;

procedure Matrix_Product is

   procedure Put (X : Real_Matrix) is
      type Fixed is delta 0.01 range -100.0..100.0;
   begin
      for I in X'Range (1) loop
         for J in X'Range (2) loop
            Put (Fixed'Image (Fixed (X (I, J))));
         end loop;
         New_Line;
      end loop;
   end Put;
   
   A : constant Real_Matrix :=
         (  ( 1.0,  1.0,  1.0,   1.0),
            ( 2.0,  4.0,  8.0,  16.0),
            ( 3.0,  9.0, 27.0,  81.0),
            ( 4.0, 16.0, 64.0, 256.0)
         );
   B : constant Real_Matrix :=
         (  (  4.0,     -3.0,      4.0/3.0,  -1.0/4.0 ),
            (-13.0/3.0, 19.0/4.0, -7.0/3.0,  11.0/24.0),
            (  3.0/2.0, -2.0,      7.0/6.0,  -1.0/4.0 ),
            ( -1.0/6.0,  1.0/4.0, -1.0/6.0,   1.0/24.0)
         );
begin
   Put (A * B);
end Matrix_Product;