MathGroup Archive 2002

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

Search the Archive

Re: How to use error message return values

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37737] Re: How to use error message return values
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sun, 10 Nov 2002 05:38:44 -0500 (EST)
  • References: <aqi79f$6a4$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Flip,

Here is an example that you may be able to modify for your own use:
It would be better to use the option Modulus for inserting the modulus to
use - since this is the way that it is done in several built-in functions.

Clear["`*"]

RIM::usage= "RIM[s,r,m],for positive integers, s, r and non-negative integer
m, constructs an s by s square matrix of integers between -r and r and tests
to see if it is invertible modulo m. If it is not invertable it announces
this in a message and invites the user to try again"

RIM[n___] :=
Module[{s, r, m, thr, mat},
    mat /;
    Which[
       Length[{n}] != 3,
           Message[RIM::argrx, RIM, Length[{n}], 3];
           False,

       !MatchQ[{n}, {(i__Integer)?Positive,  _Integer?NonNegative}],
            Message[RIM::npos];
            False,

       {s, r, m} = {n};
       mat = Table[Random[Integer, {-r, r}], {s}, {s}];
       Det[mat, Modulus -> m] == 0,
            Message[RIM::sing, mat, m];
            False,

      True, True
    ]
 ];


DEFINE MESSAGES

I also use the built-in message argrx which is described in the Help Browser
[ Other Information]

RIM::npos=
    "The first two arguments of RIM must be positive integers, the last one
\
must be a non-negative integer.";
RIM::sing=
    "the matrix `1` created by RIM was not invertible Modulo `2`, please try
\
again.";

TESTS

RIM[3,5,-2,3]

    RIM::argrx: RIM called with 4 arguments; 3 arguments are expected.

    RIM[3,5,-2,3]

RIM[3,5,-2]

    RIM::npos: The first two arguments of RIM must be positive integers, the
last \
    one must be a non-negative integer.

    RIM[3,5,-2]

RIM[3,5,0]

    {{4,-5,1},{0,3,0},{5,-3,2}}

RIM[3,5,5]

    {{1,-2,4},{4,5,4},{-4,2,1}}

RIM[3,5,5]

    RIM::sing: the matrix {{-4,-4,-1},{2,5,-1},{-3,-1,2}} created by RIM was
not \
    invertible Modulo 5, please try again.

    RIM[3,5,5]

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"flip" <flip_alpha at safebunch.com> wrote in message
news:aqi79f$6a4$1 at smc.vnet.net...
> Hello,
>
> I have a module which allows a user to definea matrix.
>
> This matrix may, of course, have an inverse or not have an inverse.
>
> As an example,
>
> In[15]:=
> c = {{5, 17}, {4, 15}};
>
> In[16]:=
> cinv = Inverse[c, Modulus -> 26]
>
> Out[16]=
> {{17, 5}, {18, 23}}
>
> In[17]:=
> c = {{5, 5}, {5, 5}};
>
> In[18]:=
> cinv = Inverse[c, Modulus -> 26]
>
> Inverse::"sing": "Matrix \!\({\(\({5, 5}\)\), \(\({5, 5}\)\)}\) is
> singular."
>
> Out[18]=
> Inverse[{{5, 5}, {5, 5}}, Modulus -> 26]
>
> How can I have my module fail in the case where an inverse does not exist?
> I want to end the module and give the user an error message stating to use
a
> new matrix: this one does not have an inverse modulo 26.
>
> How can we in general take advantage of error messages or error return
> values in order to do this?
>
> Thanks, Flip
>
> Note: remove "_alpha" to send me an email.
>
>
>




  • Prev by Date: Re: Re: OO in Mathematica
  • Next by Date: Re: Representing integers as sums of 3 squares
  • Previous by thread: Re: How to use error message return values
  • Next by thread: Re: How to use error message return values