# I'm Lisping! Help?

#### Oblivious

##### Is Kredit to Team!!
In the programming language of course.

Just starting to learn this wonderful language, and was wondering if there was anyone else here who was experienced in this language to help me evaluate my style. I was learning C before this, so this might affect how I think about the problem.

Oh, it's working correctly (I think) at the very least for 3-dimensional vectors. I can't seem to find online calculators that evaluate higher dimensions then that.

(Is there anyone who knows the proper way to evaluate 7-dimensional vectors? Is it the same as 3-dimensional?)

The code below generalizes the following formula:

c = a Χ b

c0 = a1·b2 - a2·b1
c1 = -(b2·a0 - b0·a2)
c2 = a0·b1 - a1·b0

(I know its weird to have 0 value subscripts in math, but it matches array indexing, so it is clearer.)

Code:
[SIZE=2](defun cross-product (a b)
(let ((sub-1 1))        ;Subscripts refer to starting positions
(let ((sub-2 2))    ;These starting values correspond to first c0 value
(let ((result-vector(make-array (length a))))
(dotimes (i (length result-vector))
(setf (svref result-vector i) (- (* (svref a sub-1) (svref b sub-2))
(* (svref a sub-2) (svref b sub-1))
)) ;Main computation
(setf sub-1(move sub-1 (length a)))
(setf sub-2(move sub-2 (length a))))result-vector))))
(defun move (sub size) ;Subscript increment and reset
(if (= sub (1- size)) 0 (1+ sub)))

(setf *A* #(1 2 3))[/SIZE] [SIZE=2]
(setf *B* #(6 7 8))
(print(cross-product *A* *B*))[/SIZE]
It was an interesting problem

#### Melllvar

##### Banned
There's a summation formula for calculating n-dimensional vector cross products (this may be what you're already doing in your code). There's also a trick for finding them straight from the matrix, although it isn't very pretty and I wouldn't recommend it for anything larger than three components. In fact I think it's equivalent to the determinant of the matrix, in which case any linear algebra textbook should have it listed (finding determinants of large square matrices).

As for the actual programming of it/Lisp, I haven't a clue.

#### Oblivious

##### Is Kredit to Team!!
There's a summation formula for calculating n-dimensional vector cross products (this may be what you're already doing in your code). There's also a trick for finding them straight from the matrix, although it isn't very pretty and I wouldn't recommend it for anything larger than three components. In fact I think it's equivalent to the determinant of the matrix, in which case any linear algebra textbook should have it listed (finding determinants of large square matrices).

As for the actual programming of it/Lisp, I haven't a clue.

The algorithm I wrote is just a step up from the method you would use to pull the answer straight from the matrix. I simply generalised the pattern to a loop, hoping the pattern would carry on into dimensions higher then 3.

This is, of course, naive and wrong, as I found out after a quick test for orthogonality. Dot Product != 0 as I found.

And so the search goes on.

I should note that using a loop to generalise the formula is only useful if I am dealing with variable length vectors. If I am only dealing with 3D, then its more efficient to simply hard code the formula.

Things always are harder then they seem with math. At least I'm learning something.

#### Melllvar

##### Banned
Hm, upon actually trying to figure it out, I realized that cross products only exist in 3 dimensions (and 7, apparently), and hence my previous response was completely meaningless. No doubt the majority of people viewing this thread already realized this. Oh well, I hope I at least spawned some decent chuckles or eye rolls.

It seems I was thinking of determinants, for which there's a simple recursive method for finding them for n-dimensional square matrices.

So, in short I guess I don't know how to evaluate 7-dimensional vector cross products, and probably should have just stayed out of this thread entirely. Best of luck though.

#### Oblivious

##### Is Kredit to Team!!
imo real mathematics is not about knowing the answers; it's about fumbling about in the dark in a dignified manner, which you and all scientists in existence partake in.

I guess the problem here is that I am not thinking of the cross product in a geometric way, and then looking at my algorithm and wondering why it does not work for 7-dimensions.

I've fumbled about in the dark a little while more and this is what I got:

I think the key here is orthogonality of output vectors. In the 2D context, every 2D vector has exactly one vector that is orthogonal to it, which forms a line, and in the 3D context, every 3D vector defines a 2D plane that is orthogonal to it.

In other words, every vector defines a hyperplane, one dimension lower then itself. If the cross product must return a single vector that has the same number of dimensions as the input vector, I postulate that there must be an adequate number of input vectors.

To illustrate, to find a single vector that is orthogonal to a 2D vector, I need one 2D vector, and to find a single vector that is orthogonal to a 3D vector, I need two 3D vectors.

My naive guess is that for 4D I will need three and for 7D, I will need six.

I will need to test this...

#### Oblivious

##### Is Kredit to Team!!
I was right. Damn I'm good.

At least for the 4D case, which I just tested.

Code:
(defun cross-product2(a b c)
(let ((sub-1 1))
(let ((sub-2 2))
(let ((sub-3 3))
(let ((result-vector(make-array (length a))))
(dotimes (i (length result-vector))
(setf (svref result-vector i) (- (* (svref a sub-1) (svref b sub-2)(svref c sub-3))
(* (svref a sub-3) (svref b sub-2) (svref c sub-1))
))
(setf sub-1(move sub-1 (length a)))
(setf sub-2(move sub-2 (length a)))
(setf sub-3(move sub-3 (length a))))result-vector)))))
(defun move (sub size)
(if (= sub (1- size)) 0 (1+ sub)))

(defun dot-product(a b)
(let ((result 0))
(dotimes (i (if (< (length a) (length b)) (length b) (length a)))
(setf result(+ result (* (svref a i)(svref b i)))))result))
This is the print out:
Code:
[20]> (load "Assignment 3c.lisp")
"Vector A"
#(1 2 3 4)
"Vector B"
#(2 3 4 5)
"Vector C"
#(3 4 5 6)
"A x B x C"
#(-16 20 8 -12)
"(A x B x C) . A"
0
"(A x B x C) . B"
0
"(A x B x C) . C"
0
""
;; Loaded file Assignment 3c.lisp
T
Last three zero values are dot products, indicating that we have found a vector that is perpendicular to all three 4D vectors. This validates the predictions I made regarding the required number of vectors for the cross product to work.

Now I just need to find a way to generalise this to any number of dimensions. Not nearly advanced enough in Lisp to do so, however. I suspect I would not be able to do this for a variable number of vectors in C either.

I will keep looking I guess.

#### Melllvar

##### Banned
Technically this may not get you to the cross product per say, since you're finding orthogonal vectors in dimensions that don't have a cross product. Theoretically the method you're using may not accurately generalize to the cross product in seven dimensions (or it may, I don't know - just pointing that out). I forget the technical definition of the cross product. Of course, you're at least finding sets of orthogonal vectors in other dimensions, which is interesting enough in itself.

#### Oblivious

##### Is Kredit to Team!!
Well I certainly would be stretching it to say that what I've found would be useful in any manner or that I really know what in the hell it is that I am doing.

One day I just thought it would be cool to generalise the cross product to an algorithm and this is where I went.

#### Oblivious

##### Is Kredit to Team!!
Alright, I finally got off my ass and completed my testing.

The number of vectors required to define an n-dimensional vector of n dimensions is n - 1 for dimensions below six.

In order words, the outputs of the algorithms tested positive for orthogonality only for dimensions up to five. For what reason, I have no idea yet.

Wikipedia says there are more ways to generalise the cross product to higher dimensions, and names a few algebras that are supposed to do it. Whatever that even means.

Wikipedia also tells me what I just found out:

http://en.wikipedia.org/wiki/Seven-dimensional_cross_product#Generalizations

If a first year uni undergrad can come up with it in a day, it has to be somewhere on the net I guess. However, I like my explanation better

The equations below describe the cross product for six dimensional vectors. However I cannot seem to get orthogonal sets of vectors out of it, which I can for the cases of four and five dimensions. Perhaps my initial naive algorithm has played out?

For those cases, simply remove the e vector components for five dimensions and both the e and d components for four dimensions. Of course, shorten the c components correspondingly as well.
c0 = a1·b2·c3·d4·e5 - a5·b4·c3·d2·e1
c1 = a2·b3·c4·d5·e0 - a0·b5·c4·d3·e2
c2 = a3·b4·c5·d0·e1 - a1·b0·c5·d4·e3
c3 = a4·b5·c0·d1·e2 - a2·b1·c0·d5·e4
c4 = a5·b0·c1·d2·e3 - a3·b2·c1·d0·e5
c5 = a0·b1·c2·d3·e4 - a4·b3·c2·d1·e0

I'll work on this tomorrow. For now, sleep.

#### walfin

##### Democrazy
Oooh...I didn't realise octonions were so cool!

Maybe you can generate all the 480 possible multiplication tables and compute all the relevant 7D cross products too.

#### Oblivious

##### Is Kredit to Team!!
Oooh...I didn't realise octonions were so cool!

Maybe you can generate all the 480 possible multiplication tables and compute all the relevant 7D cross products too.

A mere apprentice like me is incapable of such sorcery.

Unless you explained exactly how you wanted me to generate it. I have not really studied or have been taught n-dimensional cross products before I started this thread. Everything I write in this thread is comprised of a whole lot of guessing and testing.

I managed to generate the equations above by extending the pattern of the algorithm used to generate 3D cross products. I really have no clue why they should even work in higher dimensions. It was pure dumb luck they worked for me in 4D and 5D.

I still cannot seem to get my code for 6D to produce orthogonal vectors. So at the moment I am still trying to decide if my equations or wrong or whether I made a mistake in my code.

#### EvilScientist Trainee

And here I came thinking that you had a speech pathology.

#### walfin

##### Democrazy
Oblivious said:
Unless you explained exactly how you wanted me to generate it.
Well, I couldn't.

I didn't even know about 7D cross products until this thread.

Are you trying Scheme as well?

#### Oblivious

##### Is Kredit to Team!!
Just Common Lisp. It's like learning to program all over again. When they say functional programming they really mean functional programming. There are no operators, only functions. Everything is a function. Even the function declarations are actually functions.