Matrix multiplication

Pages: 12
Nov 1, 2014 at 4:25pm
sorry I know this is unrelated but I've d/l and extracted it.. how do I add it to bloodshed library
Nov 1, 2014 at 5:14pm
I don't use Windows or Bloodshed so I can't help. I did find this though:
http://www.bloodshed.net/faq.html#9
Nov 2, 2014 at 12:50am
Alas, I apologize, sincerely, truly. My brain must be scrambled or something. I would have sworn one of those subscripts were off last night. And I was a jerk.

:O(

Your matrix calculation looks right.

Uh, alas, I don't have time right now to mess much with your code. Can you wait a few days? (Perhaps someone else will pick it up before then.)
Nov 2, 2014 at 8:13pm
Don't worry about it we all make mistakes which is precisely the reason im in this forum.

Hopefully somebody will spot the error I sure as hell cant. The data outputted is so random like a sharp sinusoid up n down etc... I can only think that it may be that every iteration, the matrices dont initialize to their original values for some reason and so making it very random :S ..

Hope to hear back from you soon
Nov 5, 2014 at 6:02pm
Alright, sorry to take so long.

If I understand what you are trying to do correctly, the issue isn't in the multiplication operator, its in your Resultant() iteration.

Comment out line 32 in main.cpp (only multiply by m2 once, before the loop).

Also, on lines 9 and 10 of matrix.cpp you have swapped the signs. It should read

1
2
	m12= -(1/n)*sin(k*d);
	m21= n*sin(k*d);

As an aside, the *= operator should be:

1
2
3
4
5
MyMatrix& operator *= ( const MyMatrix& fred )
{
  ...
  return *this;
}

Hope this helps.

[edit] Oh, before you run that and say "what?", I modified the parameters a little to get a more sane matrix.

First, in main(), I looped over (k=0; k < 2; k+=0.001). A tenth isn't a very small sample for the wavelength of your graphs.

Second, in Resultant(), I set the 'd' arguments to 10.0 and 5.0, respectively, which is a bit more sane for viewing.

If you want I'll post an image of what I'm seeing.

Hope this helps.

[edit 2] http://home.comcast.net/~michaelthomasgreer/temp/fea12rs-wave-matrix.png
Last edited on Nov 5, 2014 at 7:11pm
Nov 10, 2014 at 10:45pm
Thanks a lot for the reply. Yeah its helped me ive been making progress. I realised for 1 thing i had the wrong expression for R all along which is ironic.

With the line 32 of main.cpp, if i comment that out surely that would mean I only multiply 1 matrix by itself several times, not multiplying them successively like such:

matrix1 = m1; matrix 2= m2;

m1*m2*m1*m2 ...

??

Nov 10, 2014 at 11:14pm
It would help significantly if we could see exactly what your assignment is.
Nov 10, 2014 at 11:41pm
By name Its the 1D transfer matrix method but ill try explain in as much detail here.

I start off with 2 matrices, where each matrix has 3 arguments(n,k and d) which are different for each matrix, n and d are fixed.
The 2 matrices are of the form given in the 1st post, I have to multiply them successively - m1*m2*m1.. a certain amount of times and have to do this over a range of k.
Given the final matrix, which is the resultant (mT) of multiplying m1 and m2 together a given number of times, I calculate the reflectivity R which is given by an expression in terms of the final matrix.

The correct expression for R is :
1
2
3
4
5
6
7
double MyMatrix::calcR()
{
	double a = (m21+m12)*(m21+m12) + (m22-m11)*(m22-m11);
	double b = (m11+m22)*(m11+m22) + (m21-m12)*(m21-m12);
	double c = a/b;
//	std::cout << "a  " << a << "asquared  " << a*a << std::endl;
	return c;


When I plot R vs k I still get a randomly sharp sinusoid type of graph over the whole range where a certain value of k should peak.


Nov 10, 2014 at 11:45pm
My apologies I have just changed the variables so that I used the actual values as opposed to scaled down ones, and a get 2 smooth curves which is a lot more satisfying.

I think i've got it solved now. Just being over analyzing when it was something minor.

Thanks again

Nov 11, 2014 at 12:04am
So you've got it solved?

(I wasn't sure what exactly you were trying to do. Each time you multiply, you are convolving the wave with the multiplicand wave. Which requires some careful observation to see the frequencies combine the way you want.)

In any case, I don't know that much about waves beyond that...
Nov 11, 2014 at 12:53am
closed account (48T7M4Gy)
If ever there was a case for using a matrix library like that from boost or matrix 11 from Techsoft and then adapting it if you want to invent something useful, this is it.

Normally the assignment operators use 'friend' to gain access to the underlying structure instead of via 'this' depending on the data structure for the matrix class but with all due respect, by not using arrays or vectors the whole scene here is one of rampant spaghetti code.

Once you get it right you can just write A *= whatever;

http://www.techsoftpl.com/matrix/ It's free, no obligation, open source and fairly easy to understand (unlike newmat).
Nov 13, 2014 at 1:04pm
Yeah i think i got it. The final graph curves are a bit more jaggedy than expected but on right track.

And kenmort i have no idea what u just said but im sure it made you feel very smart.
Nov 13, 2014 at 4:08pm
No one here is trying to feel smart. He's just suggesting a library you can use to handle all the matrix stuff without having to write it yourself.

@kemort
While he's doing something uncouth with his special-purpose matrix class, it works for the assignment at his level. Further, it doesn't meet the definition of spaghetti code, so play nice, K?
Nov 14, 2014 at 12:15am
closed account (48T7M4Gy)
@Douas I take your point even though there was no intent on my part.

I know none of us are forced to stay. If fear12rs doesn't know how to design a simple program and can't take a bit of sound advice then he may as well know it and besides, other people may learn something rather than being misled into bad practice.

True, it's not spaghettii code. On reflection I should have called it noodle-code, a sloppy mess.
Nov 14, 2014 at 2:56am
fea12rs is doing the best he can with what he has learned. No one is born writing perfect code, and for his purposes there's nothing wrong with it.
Nov 14, 2014 at 7:29am
closed account (48T7M4Gy)
@Doas

If he doesn't want to lift off from the launchpad and fizzle out then that's on fears12.

If you had bothered to take in what I wrote I gave him a pointer to the very functionality he was looking for but it's clear that two overblown egos fighting way over their weight got in the way of it.

Aside from that you only have to make your point once Douas unless of course it's to nag and have the last say, another ego failing sad to say.
Topic archived. No new replies allowed.
Pages: 12