473,382 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

multiplication of 2 matrix

50
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
Sep 18 '07 #1
12 9890
r035198x
13,262 8TB
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
And what have you done so far?
Sep 18 '07 #2
JosAH
11,448 Expert 8TB
I am a java beginner and I was given this task to do.. to create a class which given 2 matrices,and these bein of the correct dimensions, will produce a third matrix by multiplying both. There is the need of a class Matrix which contains the following methods :

public Matrix multiply (Matrix multiplicand, Matrix multiplier); and

public void display();
The display() method will output a matrix upon the console, columns should be tabbed and
rows printed on a new line e.g.
20 6 112
1 -761 12
85 30 -65
The matrix may have a any number of columns or rows. Your Matrix class must be flexible enough to support this. Generate a program which randomly creates matrices and multiplies them, always displaying each matrix (the multiplicand, multiplier and the result..)

Can please some1 help me in dealing with this task? Thanks very much
So for matrixes A and B with n rows, m columns and m rows, p columns respectively,
what would be the size of matrix C == A*B?

How would you test whether or not A and B have the correct sizes? and how
do you calculate the dot product of row i of matrix A times column j of matrix B?

What have you calculated then? Have you thought about these sub-problems?

kind regards,

Jos
Sep 18 '07 #3
sugard
50
what i know is that i need to build a matrix with 2 arrays [] []. The length of these arrays need to be randomly generated. Also the first element in the first array needs to be multiplied by the first element in the other array.

I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.

Thanks

Regards

Christine
Sep 18 '07 #4
JosAH
11,448 Expert 8TB
I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.
You do know how to program using the Java programming language don't you?

kind regards,

Jos
Sep 18 '07 #5
r035198x
13,262 8TB
what i know is that i need to build a matrix with 2 arrays [] []. The length of these arrays need to be randomly generated. Also the first element in the first array needs to be multiplied by the first element in the other array.

I dont know how to do this in java.. also i dont know how to randomly generate the length of an array.

Thanks

Regards

Christine
Before you start thinking about how to do stuff in Java, first get a Math tutorial and make sure you know how to multiply matrices manually. Note down all the special cases e.t.c
Then read a Java arrays tutorial. After that all you need to do is read the API for the java.util.Random class and you're done.

I guess you realize that the keyword in all that is read.
You can always post back here at any time that you get stuck with any of the above.
Sep 18 '07 #6
Hi,

To find the Product of two Matrices, First we've to verify the matrix sizes, for this we've a Condition like No.of Columns of First Matrix (A) should be equla to No.of Rows in Second Matrix (B) then in the resultant matrix (C), No.of Rows should be equal to the no.of Rows in First Matrix(A) and the no.of Columns of Resultant Matrix (C) should be equal to the no.of Columns of Second Matrix (B).

For example, A is a Matrix with the size of 3x2, and B is a Matrix with the size of 2x4 then the Resultant Matrix (C) size should be 3x4 (No.of Rows in A x No.of Columns in B) as These matrices satisfy the condition as no.of Columns of A are equal to No.of Rows in B)

so go ahead,

Cheers,
Sateesh.
Sep 19 '07 #7
sugard
50
I' ve done the following code.. but I dont know how to randomly generate the numbers and the length of the arrays.. The length of the arrays need to have certain restriction that the first matrix must have number of columns equal to the number of rows of the second matrix.

public class Matrix {

// the dimensions for the matrix

// Matrix dimensions
int nRowsMatrixA = 2; // number of rows for matrix A
int nColsMatrixA = 3; // number of columns for matrix A
int nRowsMatrixB = 3; // number of rows for matrix B
int nColsMatrixB = 2; // number of columns for matrix B
int nRowsMatrixC = 2; // number of rows for matrix C
int nColsMatrixC = 2; // number of columns for matrix C

double [][] matrixA = new double [nRowsMatrixA][nColsMatrixA];
double [][] matrixB = new double [nRowsMatrixA][nColsMatrixB];
double [][] matrixC = new double [nRowsMatrixC][nRowsMatrixC];

Random rand = new Random();
matrixA [0][0] = rand.nextInt(20);







public Matrix multiply (Matrix multiplicand, Matrix multiplier) {



}
}
Sep 21 '07 #8
Nepomuk
3,112 Expert 2GB
I' ve done the following code.. but I dont know how to randomly generate the numbers and the length of the arrays..
For random numbers, use java.util.Random.

About the issue of creating compatible Matrixes, you'll have to either read the lengths from the first Matrix while creating the second one or save them before creating the first Matrix and reuse them in the second one.

Greetings,
Nepomuk
Sep 21 '07 #9
axyelp
2
well i jst tried 2 get this code done fr my college tutorials!....
multiplication of matrices wid unequal orders!
works great....jst get thru d logic done!
lol...its huge...ignore d strngs fr the user plz! :P
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4. int i,j,k,x=0,ai,aj,bi,bj,a[10][10],b[10][10],c[10][10];
  5. wrong:
  6. printf("enter in the order(ixj) of matrix A\n");
  7. scanf("%d",&ai);
  8. scanf("%d",&aj);
  9. printf("\nnow the order(ixj) of matrix B\n");
  10. scanf("%d",&bi);
  11. scanf("%d",&bj);
  12. printf("\nOrder of matrix A = (%dx%d)\n",ai,aj);        //display orders of
  13. printf("Order of matrix B = (%dx%d)\n\n",bi,bj);    //of A n B!
  14.  
  15. if(aj!=bi)
  16. {
  17. printf("The resultant product matrix wont exist as Ai and Bj aren't equal!\n");
  18. printf("\n\nEnter in the orders again!\n");
  19. goto wrong;
  20. }
  21.  
  22. printf("Enter the matrix 'A'(row by row)\n\n");        //input fr matrix A!
  23. for(i=0;i<ai;i++)
  24. {
  25. printf("Row%d\n",i+1);
  26.     for(j=0;j<aj;j++)
  27.     scanf("%d",&a[i][j]);
  28. }
  29.  
  30.  
  31. printf("\nEnter the matrix 'B'(row by row)\n\n");        //input fr matrix B!
  32. for(i=0;i<bi;i++)
  33. {
  34. printf("Row%d\n",i+1);
  35.     for(j=0;j<bj;j++)
  36.     scanf("%d",&b[i][j]);
  37. }
  38.  
  39. printf("\n\n\nthe matrices of multiplication are!\n\nA:");
  40. for(i=0;i<ai;i++)                    //display A matrix!
  41. {
  42. printf("\t");
  43.     for(j=0;j<aj;j++)
  44.     printf("%d\t",a[i][j]);
  45. printf("\n\n");
  46. }
  47.  
  48. printf("\n\nB:");                                //display B matrix!
  49. for(i=0;i<bi;i++)
  50. {
  51. printf("\t");
  52.     for(j=0;j<bj;j++)
  53.     printf("%d\t",b[i][j]);
  54. printf("\n\n");
  55. }
  56.  
  57.  
  58. for(i=0;i<ai;i++)                //multiplicatn(main logic)
  59. {
  60.     for(j=0;j<bj;j++)
  61.     {
  62.     x=0;
  63.         for(k=0;k<aj;k++)
  64.         x=x+a[i][k]*b[k][j];
  65.     c[i][j]=x;
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. printf("\n\nThe multiplication of the above matrices A & B....C:\n\nA x B = ");
  72. for(i=0;i<ai;i++)                //display d product result!
  73. {
  74. printf("\t");
  75.     for(j=0;j<bj;j++)
  76.     printf("%d\t",c[i][j]);
  77. printf("\n\n\t");
  78. }
  79.  
  80. }
  81.  
Sep 30 '07 #10
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.  
Oh dear; this is a Java forum and your source code isn't valid C or C++ either.

kind regards,

Jos
Sep 30 '07 #11
i would like to know if it is possible to find a scalar r if we multiplythe matrixesM and N such that MN=rN
M= 3 -0.50 0.50 and matrix N= 6.75
0 6.50 -8 13.5
0 4 -5. 6.750
PLEASE HELP
Oct 10 '07 #12
JosAH
11,448 Expert 8TB
i would like to know if it is possible to find a scalar r if we multiplythe matrixesM and N such that MN=rN
M= 3 -0.50 0.50 and matrix N= 6.75
0 6.50 -8 13.5
0 4 -5. 6.750
PLEASE HELP
Read up on 'eigen values' and 'eigen vectors' here. btw, you should have
started your own thread for this; hijacking is considered impolite and not done.

kind regards,

Jos
Oct 10 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Michael Bader | last post by:
Hi, I'm currently working on a matrix multiplication code (matrix times matrix), and have come along some interesting/confusing results concerning the running time of the (apparently) same...
3
by: robix | last post by:
Hi again. I'm now asking your help because of a smal problem i'm getting with my multiplication matrix code. I'll try to give you as many details as possible. My matrix structure: typedef...
14
by: amitnanda | last post by:
Hi Guys, I have a matrix multiplication program in C that multiplies two matrices. When their size is 3*3 or 800*800, the program runs fine. But above that size, I get a "segmentation fault"....
0
by: lituncse | last post by:
dear friends, i have come across a problem which is difficult to solve for me.it's about starssen's matrix multiplication.in general matrix multiplication we need 8 multiplications and 4 additions...
1
emaghero
by: emaghero | last post by:
Does anybody know an algorithm for the fast multiplication of three n-x-n symmetric matrices? I have an algorithm, which I'm not too pleased with, that does the job. Does anybody know a faster...
7
by: VijaKhara | last post by:
Hi all, Is there any method which can implememt the matrix multiplication faster than using the formula as we often do by hand? I am writing the following code and my matrice: one is 3x40000 and...
6
by: amitsoni.1984 | last post by:
Hi, Is there any direct function for matrix multiplication in Python or any of its packages? or do we have to multiply element by element? Thank you, Amit
3
by: crazygrey | last post by:
Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h"...
1
by: Sozos | last post by:
Hi guys. I have a problem with writing the base case for the following matrix multiplication function I have implemented. Please help. #define index(i,j,power) (((i)<<(power))+(j)) void...
8
by: joegao1 | last post by:
can some one give me a hint? I want to program the code for matrix multiplication with as less arithmetical / multiplication operations as possible. my task is to calculate the matrix...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.