Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #12010 > unrolled thread

Triggering on exception conditions

Started byLorenz <lorenz.mueller@bluewin.ch>
First post2012-10-09 04:44 +0000
Last post2012-10-10 05:25 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Triggering on exception conditions Lorenz <lorenz.mueller@bluewin.ch> - 2012-10-09 04:44 +0000
    Re: Triggering on exception conditions awnl <awnl@gmx-topmail.de> - 2012-10-10 05:25 +0000

#12010 — Triggering on exception conditions

FromLorenz <lorenz.mueller@bluewin.ch>
Date2012-10-09 04:44 +0000
SubjectTriggering on exception conditions
Message-ID<k50a41$3os$1@smc.vnet.net>
Assume I have a statement like
Table[ {Mod[k, h], PowerMod[k, -1, h]}, {k, 1, h - 1}] 
How can I select the elements in the table which fullfill the condition 
PowerMod::ninv: 2 is not invertible modulo xxx

I would like a statement with a form like
If[PowerMod[2, -1, h] == ninv, 1, 0]
but this does not work.

More general how to catch exceptions in calculation for each type of exception condition, eg in this case PowerMod::ninv 

[toc] | [next] | [standalone]


#12023

Fromawnl <awnl@gmx-topmail.de>
Date2012-10-10 05:25 +0000
Message-ID<k530s1$910$1@smc.vnet.net>
In reply to#12010
Hi,

> Assume I have a statement like Table[ {Mod[k, h], PowerMod[k, -1,
> h]}, {k, 1, h - 1}] How can I select the elements in the table which
> fullfill the condition PowerMod::ninv: 2 is not invertible modulo
> xxx
>
> I would like a statement with a form like If[PowerMod[2, -1, h] ==
> ninv, 1, 0] but this does not work.
>
> More general how to catch exceptions in calculation for each type of
> exception condition, eg in this case PowerMod::ninv

you would, in general, use Check for this, e.g.:

Check[PowerMod[k, -1, h], $Failed, PowerMod::ninv]

but for your problem it seems simpler to just ignore these messages 
(using Quiet) and then do pattern matching with the result, making use 
of the fact that PowerMod returns unevaluated when it can't invert:

Cases[
  Quiet[
   Table[{Mod[k, h], PowerMod[k, -1, h]}, {k, 1, h - 1}],
   PowerMod::ninv
   ], {x_, _PowerMod} :> x]

This should issue additional Messages if PowerMod returns unevaluated 
for other reasons, but if you need to ensure that only those unevaluated 
PowerMod's which result from PowerMod::inv will appear you could use:

Cases[
  Quiet[
   Table[{Mod[k, h],
     Check[PowerMod[k, -1, h], $Failed, PowerMod::ninv]}, {k, 1,
     h - 1}],
   PowerMod::ninv
   ], {x_, $Failed} :> x]

hth,

albert

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web