Skip to content

matrix-cos-angle#118

Open
bdeket wants to merge 2 commits intoracket:masterfrom
bdeket:cos-angle-test
Open

matrix-cos-angle#118
bdeket wants to merge 2 commits intoracket:masterfrom
bdeket:cos-angle-test

Conversation

@bdeket
Copy link
Contributor

@bdeket bdeket commented Mar 9, 2026

possible fix for #117

  • prevent zero-matrix in test
  • better error on zero-matrix

but I'm not sure if the error/nan-branch is the best way to go about it.

* prevent zero-matrix in test
* better error on zero-matrix
(inner A* B*)]
[(or (nan? mxA) (nan? mxB) (= 0 mxA) (= 0 mxB))
(/ (matrix-dot A B) (* mxA mxB))]
(cond
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does an error message look like, when the error condition is triggered?

Maybe raise-arguments-error could be used to give a nicer error message?

@pavpanchekha
Copy link
Collaborator

This is fine but I imagine the point of the original code is to produce a matrix with infinities or NaNs in it, possibly of the correct sign.

@bdeket
Copy link
Contributor Author

bdeket commented Mar 10, 2026

  • raise-argument(s)-error: I tried this first, but since the docs don't mention anything about non-zero matrices I found the "contract violation" part of the error message incorrect. So I ended up writing an error in the style of "/: division by zero". But then it should complain about "zero matrix" and not "non-zero matrix" as I have done: I will push an update to get the error mentioned below.
  • nan's: This code only produces 1 number, I tried to keep it the same as before (racket 8.14) Looking at it again it's not completely the same:
#lang racket/base
(require math)
                                                                 ;; 8.14         now (and 9.1)
(matrix-cos-angle (matrix [[0 1]]) (matrix [[0.0 0.0]]))         ;; nan          nan
(matrix-cos-angle (matrix [[0.0 1]]) (matrix [[0 0.0]]))         ;; nan          nan
(matrix-cos-angle (matrix [[0 1]]) (matrix [[0.0 0]]))           ;; 0            0
(matrix-cos-angle (matrix [[0 1]]) (matrix [[+nan.0 0]]))        ;; 0            0
(matrix-cos-angle (matrix [[1 1]]) (matrix [[+nan.0 0]]))        ;; nan          nan
(matrix-cos-angle (matrix [[0+1i 1]]) (matrix [[+nan.0 0]]))     ;; 0+nani       nan+nani <<<<
(matrix-cos-angle (matrix [[0.+1i 1]]) (matrix [[+nan.0 0]]))    ;; nan+nani     nan+nani
(matrix-cos-angle (matrix [[1 1]]) (matrix [[+nan.0+1i 0]]))     ;; nan+nani     nan+nani
(matrix-cos-angle (matrix [[0+1i 1]]) (matrix [[+nan.0+1i 0]]))  ;; nan+nani     nan+nani
(matrix-cos-angle (matrix [[1+0i 1]]) (matrix [[0+nan.0i 0]]))   ;; 0+nani       nan+nani <<<<
(matrix-cos-angle (matrix [[1+0.i 1]]) (matrix [[0+nan.0i 0]]))  ;; nan+nani     nan+nani
(matrix-cos-angle (matrix [[1+0i 1]]) (matrix [[0 0]]))          ;; error        error (= "matrix-cos-angle: zero matrix #<array #(1 2) #[0 0]>")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants