Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2012

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

Search the Archive

Re: Triggering on exception conditions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg128366] Re: Triggering on exception conditions
  • From: awnl <awnl at gmx-topmail.de>
  • Date: Wed, 10 Oct 2012 01:25:12 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <k50a41$3os$1@smc.vnet.net>

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



  • Prev by Date: Re: Grouping graphics that are transformed together
  • Next by Date: Re: Triggering on exception conditions
  • Previous by thread: Re: Triggering on exception conditions
  • Next by thread: color function