Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can you please translate this code from java into python? I need a logic conversion that will do the same thing but only in python

can you please translate this code from java into python? I need a logic conversion that will do the same thing but only in python or the closest implementation possible. a bit of an explanation would be nice. if it's easier I added to the bottom the c++ implementation as well. I NEED this in python. I NEED you to translate form c++ or java, whatever is easier, to Python.

Please hurry up.

Here is the original question.

Write a program in Python that has both static and stack-dynamic local variables in subprograms. Create six large (at least 100 * 100) matrices in the subprogramthree static and three stack dynamic. Fill two of the static matrices and two of the stack-dynamic matrices with random numbers in the range of 1 to 100. The code in the subprogram must perform a large number of matrix multiplication operations on the static matrices and time the process. Then it must repeat this with the stack-dynamic matrices. Compare and explain the results. Explain in detail.

import java.util.Random; public class Matrices { static int staticMatrix1[][] = new int [100][100]; static int staticMatrix2[][] = new int [100][100]; static int staticMatrix3[][] = new int [100][100]; public void localMatrices() { int localMatrix1[][] = new int [100][100]; int localMatrix2[][] = new int [100][100]; int localMatrix3[][] = new int [100][100]; Random r = new Random(); for(int i =0;i<100;i++) { for(int j =0;j<100;j++) { localMatrix1[i][j] = r.nextInt(100); localMatrix2[i][j] = r.nextInt(100); } } long localStart = System.nanoTime(); multiplyLocal(localMatrix1, localMatrix2); long localEnd = System.nanoTime(); System.out.println("Time taken for local multiplication: "+ (localEnd-localStart)/ 1000000); } public void multiplyLocal(int first[][],int second[][]) { int[][] multiply = new int [100][100]; for (int c = 0; c < 100; c++) { for (int d = 0; d < 100; d++) { int sum = 0; for (int k = 0; k < 100; k++) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } } public static void multiplyStatic(int first[][],int second[][]) { int[][] multiply = new int [100][100]; for (int c = 0; c < 100; c++) { for (int d = 0; d < 100; d++) { int sum = 0; for (int k = 0; k < 100; k++) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } } public static void main(String args[]) { Random r = new Random(); for(int i = 0;i<100;i++) { for(int j = 0;j<100;j++) { staticMatrix1[i][j] = r.nextInt(100); staticMatrix2[i][j] = r.nextInt(100); } } long staticStart = System.nanoTime(); multiplyStatic(staticMatrix1, staticMatrix2); long staticEnd = System.nanoTime(); System.out.println("Time taken for static multiplication: "+ (staticEnd-staticStart)/ 1000000); Matrices matrices = new Matrices(); matrices.localMatrices(); } } *************************************** Outputs: Time taken for static multiplication: 6 Time taken for local multiplication: 12 **********************C++**********************

// Program demonstrates stack and

// static multidimensional array

#include

#include

#include

#include

using namespace std;

Create a class demo to store the Stack-dynamic local variable.

//Class definition

class demo

{

public:

Create a method sdarray() to assign values to the array elements one by one by executing a nested loop.

void sdarray()

/* array that initializes stack dynamic array and performs matrix multiplication on them */

{

int arr4[100][100];

int arr5[100][100];

int arr6[100][100];

//Assign values to the matrices

for(int i=0;i<100;i++)

{

for(int j=0;j<100;j++)

{

arr4[i][j]=rand()%100 +1;

arr5[i][j]=rand()%100 +1;

arr6[i][j]=rand()%100 +1;

}

}

// create an array to store the result

int result[100][100];

for(int i=0;i< 100;i++)

{

for(int j=0;j< 100;j++)

{

result[i][j] = 0;

for(int k=0;k< 100;k++)

{

result[i][j] = result[i][j] +

arr4[i][k] *

arr5[k][j];

}

}

}

}

}; // end of class demo

In the main(), define static type matrices and perform multiplication on them. Record the execution time for both the multiplication operations for static as well as for stack-dynamic matrices. Display both the execution times.

int main()

{

static int arr1[100][100];

static int arr2[100][100];

static int arr3[100][100];

int result[100][100];

demo ob;

The clock_t defines a variable which can store time values.

clock_t t;

t=clock();

ob.sdarray(); // call to member function of class demo

t=clock()-t;

cout<<" Time consumed in Processing Stack-dynamic

variable Matrix:"<<((float)t)/CLOCKS_PER_SEC

<<" sec";

t=clock();

for(int i=0;i<100;i++)

{

for(int j=0;j<100;j++)

{

//initializing array elements using

//random function

arr1[i][j]=rand()%100 +1;

arr2[i][j]=rand()%100 +1;

arr3[i][j]=rand()%100 +1;

}

}

for(int i=0;i< 100;i++)

{

for(int j=0;j< 100;j++)

{

result[i][j] = 0;

for(int k=0;k< 100;k++)

{

result[i][j] = result[i][j] +

arr1[i][k] *

arr2[k][j];

}

}

}

t=clock()-t;

cout<<" Time consumed in Processing Static variable

Matrix:"<<((float)t)/CLOCKS_PER_SEC

<<" sec";

getch();

return 0;

} // end of main function

Sample Output:

Time consumed in Processing Stack-dynamic variable Matrix:0.008 sec

Time consumed in Processing Static variable Matrix:0.009 sec

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 3 Lnai 8726

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448440, 978-3662448441

More Books

Students also viewed these Databases questions

Question

2. Develop a persuasive topic and thesis

Answered: 1 week ago

Question

1. Define the goals of persuasive speaking

Answered: 1 week ago