Opened 10 years ago
Closed 10 years ago
#10545 closed enhancement (fixed)
Add the outer product of two vectors
Reported by: | rbeezer | Owned by: | jason, was |
---|---|---|---|
Priority: | minor | Milestone: | sage-4.7 |
Component: | linear algebra | Keywords: | |
Cc: | jhpalmieri | Merged in: | sage-4.7.alpha3 |
Authors: | Rob Beezer | Reviewers: | Felix Lawrence, John Palmieri |
Report Upstream: | N/A | Work issues: | |
Branch: | Commit: | ||
Dependencies: | Stopgaps: |
Description (last modified by )
Title pretty much says it all. Students frequently can't tell an inner product from an outer product (probably not too careful about where a transpose is). Maybe this will help.
Depends on #10541
Apply trac_10545-vector-outer-product.patch, trac_10545-vector-outer-product-doc.patch
Attachments (2)
Change History (17)
comment:1 Changed 10 years ago by
- Description modified (diff)
- Status changed from new to needs_review
comment:2 in reply to: ↑ description ; follow-up: ↓ 3 Changed 10 years ago by
I didn't know that is sometimes used as a definition of "outer product", but Wikipedia backs you up on it.
In other languages, "exterior product" translates to the same word as "outer product", so a -1 from me for having this definition of outer product. I think this term will be a source of confusion.
If someone needs the tensor product, it is easy enough to get via the one-liner
lambda v,w: matrix(len(v),len(w),[a*b for a in v for b in w])
(given Sage's preference for row vectors versus the preference of most LA texts for column vectors, I expect that relating inner/outer products to a question of where to put the transpose is only going to cause *more* confusion in students, by the way)
comment:3 in reply to: ↑ 2 ; follow-up: ↓ 4 Changed 10 years ago by
- Description modified (diff)
Hi Nils,
Thanks for the comments. Since vectors are neither rows nor columns in Sage, any notion of a transpose is irrelevant. The outer product just is what it is. I could delete the mention of the transpose in the mathematical description in the docstring.
"Someone" are 19-year-old students, who shouldn't need to understand a lambda function to learn linear algebra.
Sage can work well with a column-oriented approach - it just needs a few things. Check out:
http://wiki.sagemath.org/devel/LatexToWorksheet
Rob
Replying to nbruin:
I didn't know that is sometimes used as a definition of "outer product", but Wikipedia backs you up on it.
In other languages, "exterior product" translates to the same word as "outer product", so a -1 from me for having this definition of outer product. I think this term will be a source of confusion.
If someone needs the tensor product, it is easy enough to get via the one-liner
lambda v,w: matrix(len(v),len(w),[a*b for a in v for b in w])(given Sage's preference for row vectors versus the preference of most LA texts for column vectors, I expect that relating inner/outer products to a question of where to put the transpose is only going to cause *more* confusion in students, by the way)
comment:4 in reply to: ↑ 3 Changed 10 years ago by
"Someone" are 19-year-old students, who shouldn't need to understand a lambda function to learn linear algebra.
Slightly off-topic: If you don't want lambda and v,w are the vectors you want to take the kronecker product of:
matrix([[a*b for b in w] for a in v])
(to avoid depending on the order in which python iterates through a multiple "for")
On-topic: In Dutch "uitprodukt" and in German "auseres produkt" is "exterior product" (as in "wedge product" or "cross product"). These terms literally translate to "outer product". I don't know about french. That's why I think "outer product" is better avoided, because it likely causes confusion.
(truly off-topic: Do you think you are doing 19-year olds a service by teaching them kronecker products of column x row vectors? Cross products of 3-dim vectors are probably much more useful for them and having to juggle 3 (instead of 2) ways of taking products of vectors is probably more confusing for them)
comment:5 Changed 10 years ago by
NumPy
calls it the outer product.
http://docs.scipy.org/doc/numpy/reference/generated/numpy.outer.html#numpy.outer
comment:6 Changed 10 years ago by
- Reviewers set to Felix Lawrence
- Status changed from needs_review to positive_review
This definition of outer product was what I was taught, and it seems to be the standard terminology.
The code works and passes long doctests, and is well documented. Positive review.
comment:7 Changed 10 years ago by
- Status changed from positive_review to needs_work
The ticket number in the commit message is wrong.
Changed 10 years ago by
comment:8 Changed 10 years ago by
- Status changed from needs_work to positive_review
Jeroen - sorry for the trouble. Patch replaced with a fixed one.
comment:9 Changed 10 years ago by
- Status changed from positive_review to needs_work
During a trial merge of sage-4.7.alpha3, I get the following doctest error:
sage -t -force_lib devel/sage/sage/modules/free_module_element.pyx ********************************************************************** File "/mnt/usb1/scratch/jdemeyer/merger/sage-4.7.alpha3/devel/sage-main/sage/modules/free_module_element.pyx", line 2292: sage: z = w.outer_product(v) Expected: Traceback (most recent call last): ... TypeError: unsupported operand parent(s) for '*': 'Full MatrixSpace of 2 by 1 dense matrices over Rational Field' and 'Full MatrixSpac e of 1 by 4 dense matrices over Finite Field of size 7' Got: Traceback (most recent call last): File "/mnt/usb1/scratch/jdemeyer/merger/sage-4.7.alpha3/local/bin/ncadoctest.py", line 1231, in run_one_test self.run_one_example(test, example, filename, compileflags) File "/mnt/usb1/scratch/jdemeyer/merger/sage-4.7.alpha3/local/bin/sagedoctest.py", line 38, in run_one_example OrigDocTestRunner.run_one_example(self, test, example, filename, compileflags) File "/mnt/usb1/scratch/jdemeyer/merger/sage-4.7.alpha3/local/bin/ncadoctest.py", line 1172, in run_one_example compileflags, 1) in test.globs File "<doctest __main__.example_57[27]>", line 1, in <module> z = w.outer_product(v)###line 2292: sage: z = w.outer_product(v) File "free_module_element.pyx", line 2307, in sage.modules.free_module_element.FreeModuleElement.outer_product (sage/modules/free_mo dule_element.c:12527) return self.column()*right.row() File "element.pyx", line 2282, in sage.structure.element.Matrix.__mul__ (sage/structure/element.c:15874) File "coerce.pyx", line 709, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:6368) File "action.pyx", line 139, in sage.matrix.action.MatrixMatrixAction._call_ (sage/matrix/action.c:2742) File "matrix_rational_dense.pyx", line 1331, in sage.matrix.matrix_rational_dense.Matrix_rational_dense.change_ring (sage/matrix/mat rix_rational_dense.c:13867) File "element.pyx", line 1551, in sage.structure.element.RingElement.__div__ (sage/structure/element.c:12720) File "coerce.pyx", line 713, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:6426) File "element.pyx", line 1549, in sage.structure.element.RingElement.__div__ (sage/structure/element.c:12694) File "integer_mod.pyx", line 2223, in sage.rings.finite_rings.integer_mod.IntegerMod_int._div_ (sage/rings/finite_rings/integer_mod. c:19384) ZeroDivisionError: Inverse does not exist. **********************************************************************
comment:10 Changed 10 years ago by
- Cc jhpalmieri added
- Description modified (diff)
- Status changed from needs_work to needs_review
Thanks, Jeroen. Coercion/error-trapping got changed somehow, and I've asked about it on sage-devel. New patch just adjusts the doctest to a pair of rings that behave better and make the same point, and this is really a matrix multiplication error message anyway.
I'll see if I can get a review here at Sage Days 29. ;-)
Rob
comment:11 Changed 10 years ago by
Should this be compared with the "tensor_product" method of matrices? Should "outer_product" have "tensor_product" as a synonym? (According to wikipedia, it should.) For reasons I don't understand, the tensor product of matrices comes equipped with subdivisions, but other than that,
sage: v.outer_product(w)
and
sage: m1 = matrix(v).transpose() sage: m2 = matrix(w) sage: m1.tensor_product(m2)
should produce the same thing.
Changed 10 years ago by
comment:12 Changed 10 years ago by
- Reviewers changed from Felix Lawrence to Felix Lawrence, John Palmieri
Replacement "doc" patch is still almost all documentation, but now includes a "tensor_product" synonym as suggested.
New doctest compares it with the matrix version of a tensor product.
comment:13 Changed 10 years ago by
Latest addition passes all long tests, so is ready for review.
comment:14 Changed 10 years ago by
- Status changed from needs_review to positive_review
comment:15 Changed 10 years ago by
- Merged in set to sage-4.7.alpha3
- Resolution set to fixed
- Status changed from positive_review to closed
For anybody trying this out, it really only needs the row/column patch on #10541. You will get one documentation error, but that is fixed on the other half of #10541.
Patchbot:
Depends on #10541