Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'column': 0.07; 'smallest': 0.07; '#print': 0.09; 'calculating': 0.09; 'happen?': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; '"getting': 0.16; "'''": 0.16; '1e-100': 0.16; 'multiplied': 0.16; 'pivot': 0.16; 'subject:ever': 0.16; 'zero,': 0.16; 'zero.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User- Agent:1': 0.23; 'received:emailsrvr.com': 0.24; 'received:(smtp server)': 0.26; 'code:': 0.26; 'gets': 0.27; 'van': 0.27; 'header :In-Reply-To:1': 0.27; 'point': 0.28; 'appreciated.': 0.29; 'am,': 0.29; 'gives': 0.31; 'code': 0.31; 'assert': 0.31; 'gary': 0.31; 'becomes': 0.33; 'but': 0.35; 'subject:?': 0.36; 'easily': 0.37; 'to:addr:python-list': 0.38; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'skip:- 60': 0.39; 'how': 0.40; 'remove': 0.60; 'break': 0.61; 'such': 0.63; 'due': 0.66; 'hints': 0.68; 'subject:this': 0.83; 'determinant': 0.84; 'float,': 0.84; 'matrix.': 0.84; 'multiplying': 0.84; 'albert': 0.91; 'trouble.': 0.91 X-Virus-Scanned: OK Date: Sat, 10 May 2014 08:48:17 -0700 From: Gary Herron User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How can this assert() ever trigger? References: <536e44c1$0$27147$e4fe514c@dreader35.news.xs4all.nl> In-Reply-To: <536e44c1$0$27147$e4fe514c@dreader35.news.xs4all.nl> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399737403 news.xs4all.nl 2860 [2001:888:2000:d::a6]:44446 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71252 On 05/10/2014 08:24 AM, 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. Easily due to *underflow* precision trouble. Your "result" may never be zero, but it can be very small. Take the product of many of such tiny values, and the result can be less then the smallest value representable by a float, at which point it becomes zero. To see this clearly, try this Python code: >>> a = 1.0 >>> while a > 0: ... a = a*1.0e-50 ... print(a) ... 1e-50 1e-100 1e-150 1e-200 1e-250 1e-300 0.0 Gary Herron > > Any hints appreciated. > > Groetjes Albert