MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Assertions in Mathematica?

On 10/28/2010 1:26 AM, kj wrote:
> What's the best to implement assertions in Mathematica?  By assertions
> I mean statements like assert(exp) in C, which generate an error
> if exp evaluates to false.
> This *should* be trivial, but it's Mathematica, so...
> Naively, I tried defining:
> Assert[exp_, msg__] := If[!exp, Message[msg]; Abort[]]

Hello KJ;

May be a build-in Assert function would be nice. But what I do now is 
simply check, at the start of the function, manually, using an If[] 
statement, on all the "asserts" or conditions that I want satisfied, and 
if a condition fail, I Throw a message.

Nothing fancy, but it works well, and it seems to satisfy the need of 
what Assert is supposed to do?

Here is an example code I just wrote last night for a school HW, I was 
making a function to check for DD matrix and wanted to make sure the 
matrix was square, so wanted to assert that a matrix is square, and I 
looked to see if there was a MatrixSquareQ built-in Mathematica, but 
there was not, so I typed:



(* assert matrix is square *)

If[nRow!= nCol,
   Throw["diagonalDominantMatrixQ:: Matrix must be square"]


How is something like the above different from saying

    Assert[matrixIsSquare[mat]==True, error.....]

in terms of its final effect it will have on behavior of program? aren't 
these equivalent?

> ...which, of course, failed to work (as I've learned to expect);
> instead it produced a cryptic error "Message::name : Message name
> ... is not of the form symbol::name or symbol::name::language."
> I tried many other things, but after wasting 1 hour on this
> ridiculously trivial programming task, I'm reduced to begging for
> help.  (This, by the way, is always the way it is with me and
> Mathematica, and I've been using it on-and-off for almost 20 years.
> The documentation is as useless to me today as it was 20 years ago.
> I find it as horrible as the rest of Mathematica is brilliant.)
> I've posted desperate questions over programming mind-numbing
> trivialities like this one in Mathematica before, i.e. questions
> that seem so elementary and fundamental that no one who has access
> to the documentation and who can read should *ever* have to ask
> them.  I ask them less wanting to get the answer to the questions
> themselves than hoping to learn how I could have answered such
> questions by myself.  But I've never found how to do this.  Those
> who know the answers *already* can give them to me if they feel so
> inclined.  (And how they got to know the answer to begin with, I
> don't know; I imagine it took years of sustained Mathematica
> programming.  Or maybe they asked a similar question before to
> someone who already knew the answer...)  But no one has been able
> to tell me how someone who *doesn't* know the answers to such questions
> already can figure it out without outside help.
> But hope springs eternal!  If someone is kind enough to tell me
> how I could implement my Assert, I'd be most grateful.  If someone
> can tell me how I could have arrived at this answer by myself by
> consulting the documentation, I'd be ecstatic.
> TIA,
> kj


  • Prev by Date: Re: Assertions in Mathematica?
  • Next by Date: Re: Assertions in Mathematica?
  • Previous by thread: Re: Assertions in Mathematica?
  • Next by thread: Re: Assertions in Mathematica?