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


Groups > comp.lang.python > #71250

Re: How can this assert() ever trigger?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'column': 0.07; '#print': 0.09; 'calculating': 0.09; 'happen?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; 'def': 0.12; '"getting': 0.16; "'''": 0.16; '323': 0.16; 'multiplied': 0.16; 'pivot': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:ever': 0.16; 'zero.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'van': 0.27; 'point': 0.28; 'appreciated.': 0.29; 'gives': 0.31; 'code': 0.31; '"",': 0.31; 'assert': 0.31; 'file': 0.32; '(most': 0.33; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'received:org': 0.40; 'how': 0.40; 'remove': 0.60; 'break': 0.61; 'hints': 0.68; 'subject:this': 0.83; 'determinant': 0.84; 'matrix.': 0.84; 'multiplying': 0.84; 'albert': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: How can this assert() ever trigger?
Date Sat, 10 May 2014 17:50 +0200
Organization None
References <536e44c1$0$27147$e4fe514c@dreader35.news.xs4all.nl>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd8b0b.dip0.t-ipconnect.de
User-Agent KNode/4.11.5
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.9850.1399737019.18130.python-list@python.org> (permalink)
Lines 67
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1399737019 news.xs4all.nl 2919 [2001:888:2000:d::a6]:41305
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71250

Show key headers only | View raw


Albert van der Horst wrote:

> I have the following code for calculating the determinant of
> a matrix. It works inasfar that it gives the same result as an
> octave program on a same matrix.
> 
> / ----------------------------------------------------------------
> 
> def determinant( mat ):
>     ''' Return the determinant of the n by n matrix mat
>         i row j column
>         Destroys mat ! '''
>     #print "getting determinat of", mat
>     n=len(mat)
>     nom = 1.
>     if n == 1: return mat[0][0]
>     lastr = mat.pop()
>     jx=-1
>     for j in xrange(n):
>        if lastr[j]:
>            jx=j
>            break
>     if jx==-1: return 0.
>     result = lastr[jx]
>     assert(result<>0.)
>     # Make column jx zero by subtracting a multiple of the last row.
>     for i in xrange(n-1):
>         pivot = mat[i][jx]
>         if 0. == pivot: continue
>         assert(result<>0.)
>         nom *= result   # Compenstate for multiplying a row.
>         for j in xrange(n):
>             mat[i][j] *= result
>         for j in xrange(n):
>             mat[i][j] -= pivot*lastr[j]
>     # Remove colunm jx
>     for i in xrange(n-1):
>        x= mat[i].pop(jx)
>        assert( x==0 )
> 
>     if (n-1+jx)%2<>0: result = -result
>     det = determinant( mat )
>     assert(nom<>0.)
>     return result*det/nom
> 
> /-----------------------------------------
> 
> Now on some matrices the assert triggers, meaning that nom is zero.
> How can that ever happen? mon start out as 1. and gets multiplied
> with a number that is asserted to be not zero.
> 
> Any hints appreciated.

Floating point precision is limited:

>>> x = 1.0
>>> for i in itertools.count():
...     x *= .1
...     assert x
... 
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
AssertionError
>>> i
323

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How can this assert() ever trigger? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-05-10 15:24 +0000
  Re: How can this assert() ever trigger? Peter Otten <__peter__@web.de> - 2014-05-10 17:50 +0200
  Re: How can this assert() ever trigger? Ethan Furman <ethan@stoneleaf.us> - 2014-05-10 08:35 -0700
  Re: How can this assert() ever trigger? Gary Herron <gary.herron@islandtraining.com> - 2014-05-10 08:48 -0700
  Re: How can this assert() ever trigger? Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2014-05-10 17:56 +0200
    Re: How can this assert() ever trigger? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-05-10 16:39 +0000
  Re: How can this assert() ever trigger? Joseph Martinot-Lagarde <joseph.martinot-lagarde@m4x.org> - 2014-05-12 19:05 +0200
    Re: How can this assert() ever trigger? albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-05-13 09:56 +0000
      Re: How can this assert() ever trigger? Joseph Martinot-Lagarde <joseph.martinot-lagarde@m4x.org> - 2014-05-14 01:14 +0200

csiph-web