WeixiangYan/CodeTransOcean huggingface dataset:
Task: Identity matrix

C: #include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
   if (argc < 2) {
      printf("usage: identitymatrix <number of rows>\n");
      exit(EXIT_FAILURE);
   }
   int rowsize = atoi(argv[1]);
   if (rowsize < 0) {
      printf("Dimensions of matrix cannot be negative\n");
      exit(EXIT_FAILURE);
   }
   int numElements = rowsize * rowsize;
   if (numElements < rowsize) {
      printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements);
      abort();
   }
   int** matrix = calloc(numElements, sizeof(int*));
   if (!matrix) {
      printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int*));
      abort();
   }
   for (unsigned int row = 0;row < rowsize;row++) {
      matrix[row] = calloc(numElements, sizeof(int));
      if (!matrix[row]) {
         printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int));
         abort();
      }
      matrix[row][row] = 1;
   }
   printf("Matrix is: \n");
   for (unsigned int row = 0;row < rowsize;row++) {
      for (unsigned int column = 0;column < rowsize;column++) {
         printf("%d ", matrix[row][column]);
      }
      printf("\n");
   }
}








VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}


C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}



Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}




Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}






Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}







VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix



C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}


Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}
Task: Identity matrix



C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}



Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}
Task: Identity matrix



C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}





Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""
Task: Identity matrix



C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}






VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix




Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}


Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}
Task: Identity matrix





Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}



Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""
Task: Identity matrix





Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}




VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix




Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}





VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix







Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""


VB: Private Function Identity(n As Integer) As Variant
    Dim I() As Integer
    ReDim I(n - 1, n - 1)
    For j = 0 To n - 1
        I(j, j) = 1
    Next j
    Identity = I
End Function
Task: Identity matrix

C: #include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
   if (argc < 2) {
      printf("usage: identitymatrix <number of rows>\n");
      exit(EXIT_FAILURE);
   }
   int rowsize = atoi(argv[1]);
   if (rowsize < 0) {
      printf("Dimensions of matrix cannot be negative\n");
      exit(EXIT_FAILURE);
   }
   int numElements = rowsize * rowsize;
   if (numElements < rowsize) {
      printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements);
      abort();
   }
   int** matrix = calloc(numElements, sizeof(int*));
   if (!matrix) {
      printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int*));
      abort();
   }
   for (unsigned int row = 0;row < rowsize;row++) {
      matrix[row] = calloc(numElements, sizeof(int));
      if (!matrix[row]) {
         printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int));
         abort();
      }
      matrix[row][row] = 1;
   }
   printf("Matrix is: \n");
   for (unsigned int row = 0;row < rowsize;row++) {
      for (unsigned int column = 0;column < rowsize;column++) {
         printf("%d ", matrix[row][column]);
      }
      printf("\n");
   }
}





Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}
Task: Identity matrix




Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}




Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""
Task: Identity matrix

C: #include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
   if (argc < 2) {
      printf("usage: identitymatrix <number of rows>\n");
      exit(EXIT_FAILURE);
   }
   int rowsize = atoi(argv[1]);
   if (rowsize < 0) {
      printf("Dimensions of matrix cannot be negative\n");
      exit(EXIT_FAILURE);
   }
   int numElements = rowsize * rowsize;
   if (numElements < rowsize) {
      printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements);
      abort();
   }
   int** matrix = calloc(numElements, sizeof(int*));
   if (!matrix) {
      printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int*));
      abort();
   }
   for (unsigned int row = 0;row < rowsize;row++) {
      matrix[row] = calloc(numElements, sizeof(int));
      if (!matrix[row]) {
         printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int));
         abort();
      }
      matrix[row][row] = 1;
   }
   printf("Matrix is: \n");
   for (unsigned int row = 0;row < rowsize;row++) {
      for (unsigned int column = 0;column < rowsize;column++) {
         printf("%d ", matrix[row][column]);
      }
      printf("\n");
   }
}






PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));
Task: Identity matrix



C#: using System;
using System.Linq;

namespace IdentityMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Requires exactly one argument");
                return;
            }
            int n;
            if (!int.TryParse(args[0], out n))
            {
                Console.WriteLine("Requires integer parameter");
                return;
            }

            var identity =
                Enumerable.Range(0, n).Select(i => Enumerable.Repeat(0, n).Select((z,j) => j == i ? 1 : 0).ToList()).ToList();
            foreach (var row in identity)
            {
                foreach (var elem in row)
                {
                    Console.Write(" " + elem);
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }
}




PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));
Task: Identity matrix


C++: template<class T>
class matrix
{
public:
    matrix( unsigned int nSize ) : 
      m_oData(nSize * nSize, 0), m_nSize(nSize) {}

      inline T& operator()(unsigned int x, unsigned int y)
      {
          return m_oData[x+m_nSize*y];
      }

      void identity()
      {
          int nCount = 0;
          int nStride = m_nSize + 1;
          std::generate( m_oData.begin(), m_oData.end(), 
              [&]() { return !(nCount++%nStride); } );
      }

      inline unsigned int size() { return m_nSize; }

private:
    std::vector<T>    m_oData;
    unsigned int      m_nSize;
};

int main()
{
    int nSize;
    std::cout << "Enter matrix size (N): ";
    std::cin >> nSize;

    matrix<int> oMatrix( nSize );

    oMatrix.identity();

    for ( unsigned int y = 0; y < oMatrix.size(); y++ )
    {
        for ( unsigned int x = 0; x < oMatrix.size(); x++ )
        {
            std::cout << oMatrix(x,y) << " ";
        }
        std::cout << std::endl;
    }
    return 0;
}





PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));
Task: Identity matrix




Go: package main

import (
    "fmt"

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

func eye(n int) *mat.Dense {
    m := mat.NewDense(n, n, nil)
    for i := 0; i < n; i++ {
        m.Set(i, i, 1)
    }
    return m
}

func main() {
    fmt.Println(mat.Formatted(eye(3)))
}



PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));
Task: Identity matrix





Java: public class PrintIdentityMatrix {

    public static void main(String[] args) {
        int n = 5;
        int[][] array = new int[n][n];

        IntStream.range(0, n).forEach(i -> array[i][i] = 1);

        Arrays.stream(array)
                .map((int[] a) -> Arrays.toString(a))
                .forEach(System.out::println);
    }
}


PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));
Task: Identity matrix






PHP: function identity($length) {
	return array_map(function($key, $value) {$value[$key] = 1; return $value;}, range(0, $length-1),
	array_fill(0, $length, array_fill(0,$length, 0)));
}
function print_identity($identity) {
	echo implode(PHP_EOL, array_map(function ($value) {return implode(' ', $value);}, $identity));
}
print_identity(identity(10));


Python: def identity(size):
    matrix = [[0]*size for i in range(size)]
    

    for i in range(size):
        matrix[i][i] = 1
    
    for rows in matrix:
        for elements in rows:
            print elements,
        print ""