From patchwork Thu Jun 16 21:37:13 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jesse Barker X-Patchwork-Id: 2014 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 5567023F3D for ; Thu, 16 Jun 2011 21:37:16 +0000 (UTC) Received: from mail-vx0-f180.google.com (mail-vx0-f180.google.com [209.85.220.180]) by fiordland.canonical.com (Postfix) with ESMTP id 06CE5A18472 for ; Thu, 16 Jun 2011 21:37:15 +0000 (UTC) Received: by mail-vx0-f180.google.com with SMTP id 12so2157025vxk.11 for ; Thu, 16 Jun 2011 14:37:15 -0700 (PDT) Received: by 10.52.175.197 with SMTP id cc5mr1955811vdc.287.1308260235738; Thu, 16 Jun 2011 14:37:15 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.52.183.130 with SMTP id em2cs213999vdc; Thu, 16 Jun 2011 14:37:15 -0700 (PDT) Received: by 10.227.205.12 with SMTP id fo12mr1378064wbb.70.1308260233698; Thu, 16 Jun 2011 14:37:13 -0700 (PDT) Received: from adelie.canonical.com (adelie.canonical.com [91.189.90.139]) by mx.google.com with ESMTP id e5si1435705wbh.113.2011.06.16.14.37.13; Thu, 16 Jun 2011 14:37:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) client-ip=91.189.90.139; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.139 as permitted sender) smtp.mail=bounces@canonical.com Received: from loganberry.canonical.com ([91.189.90.37]) by adelie.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1QXKFV-0003w5-1R for ; Thu, 16 Jun 2011 21:37:13 +0000 Received: from loganberry.canonical.com (localhost [127.0.0.1]) by loganberry.canonical.com (Postfix) with ESMTP id 04A2A2E8950 for ; Thu, 16 Jun 2011 21:37:13 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: libmatrix X-Launchpad-Branch: ~jesse-barker/libmatrix/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 16 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~jesse-barker/libmatrix/trunk] Rev 16: Fix inverse method (minors were constructed incorrectly). In addition to the Message-Id: <20110616213713.13415.24025.launchpad@loganberry.canonical.com> Date: Thu, 16 Jun 2011 21:37:13 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13216"; Instance="initZopeless config overlay" X-Launchpad-Hash: 1392a9c8a59b9f44d82034f5eaf7d2c1f75f54b9 ------------------------------------------------------------ revno: 16 committer: Jesse Barker branch nick: trunk timestamp: Thu 2011-06-16 14:22:50 -0700 message: Fix inverse method (minors were constructed incorrectly). In addition to the fixes required on the method, the test code (to be added shortly) begged the addition of equal and not-equal operators for matrices as well as fixing the print methods. modified: mat.h --- lp:libmatrix https://code.launchpad.net/~jesse-barker/libmatrix/trunk You are subscribed to branch lp:libmatrix. To unsubscribe from this branch go to https://code.launchpad.net/~jesse-barker/libmatrix/trunk/+edit-subscription === modified file 'mat.h' --- mat.h 2011-06-14 23:08:48 +0000 +++ mat.h 2011-06-16 21:22:50 +0000 @@ -98,10 +98,14 @@ { throw std::runtime_error("Matrix is noninvertible!!!!"); } - m_[0] /= d; - m_[1] /= d; - m_[2] /= d; - m_[3] /= d; + T c0r0(m_[3] / d); + T c0r1(-m_[1] / d); + T c1r0(-m_[2] / d); + T c1r1(m_[0] / d); + m_[0] = c0r0; + m_[1] = c0r1; + m_[2] = c1r0; + m_[3] = c1r1; return *this; } @@ -110,20 +114,33 @@ static const int precision(6); // row 0 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][0]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][1]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][0]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][1]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; std::cout << " |" << std::endl; } operator const T*() const { return &m_[0];} + bool operator==(const tmat2& rhs) const + { + return m_[0] == rhs.m_[0] && + m_[1] == rhs.m_[1] && + m_[2] == rhs.m_[2] && + m_[3] == rhs.m_[3]; + } + + bool operator!=(const tmat2& rhs) const + { + return !(*this == rhs); + } + tmat2& operator=(const tmat2& rhs) { if (this != &rhs) @@ -337,13 +354,13 @@ throw std::runtime_error("Matrix is noninvertible!!!!"); } tmat2 minor0(m_[4], m_[5], m_[7], m_[8]); - tmat2 minor1(m_[3], m_[5], m_[6], m_[8]); - tmat2 minor2(m_[3], m_[4], m_[6], m_[7]); - tmat2 minor3(m_[1], m_[2], m_[7], m_[8]); + tmat2 minor1(m_[7], m_[8], m_[1], m_[2]); + tmat2 minor2(m_[1], m_[2], m_[4], m_[5]); + tmat2 minor3(m_[6], m_[8], m_[3], m_[5]); tmat2 minor4(m_[0], m_[2], m_[6], m_[8]); - tmat2 minor5(m_[0], m_[1], m_[6], m_[7]); - tmat2 minor6(m_[1], m_[2], m_[4], m_[5]); - tmat2 minor7(m_[0], m_[2], m_[3], m_[5]); + tmat2 minor5(m_[3], m_[5], m_[0], m_[2]); + tmat2 minor6(m_[3], m_[4], m_[6], m_[7]); + tmat2 minor7(m_[6], m_[7], m_[0], m_[1]); tmat2 minor8(m_[0], m_[1], m_[3], m_[4]); m_[0] = minor0.determinant() / d; m_[1] = minor1.determinant() / d; @@ -362,32 +379,50 @@ static const int precision(6); // row 0 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][2]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[6]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][2]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[4]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[7]; std::cout << " |" << std::endl; // row 2 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][2]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[5]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[8]; std::cout << " |" << std::endl; } operator const T*() const { return &m_[0];} + bool operator==(const tmat3& rhs) const + { + return m_[0] == rhs.m_[0] && + m_[1] == rhs.m_[1] && + m_[2] == rhs.m_[2] && + m_[3] == rhs.m_[3] && + m_[4] == rhs.m_[4] && + m_[5] == rhs.m_[5] && + m_[6] == rhs.m_[6] && + m_[7] == rhs.m_[7] && + m_[8] == rhs.m_[8]; + } + + bool operator!=(const tmat3& rhs) const + { + return !(*this == rhs); + } + tmat3& operator=(const tmat3& rhs) { if (this != &rhs) @@ -654,20 +689,23 @@ throw std::runtime_error("Matrix is noninvertible!!!!"); } tmat3 minor0(m_[5], m_[6], m_[7], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); - tmat3 minor1(m_[4], m_[6], m_[7], m_[8], m_[10], m_[11], m_[12], m_[14], m_[15]); - tmat3 minor2(m_[4], m_[5], m_[7], m_[8], m_[9], m_[11], m_[12], m_[13], m_[15]); - tmat3 minor3(m_[4], m_[5], m_[6], m_[8], m_[9], m_[10], m_[12], m_[13], m_[14]); - tmat3 minor4(m_[1], m_[2], m_[3], m_[9], m_[10], m_[11], m_[13], m_[14], m_[15]); + tmat3 minor1(m_[1], m_[2], m_[3], m_[13], m_[14], m_[15], m_[9], m_[10], m_[11]); + tmat3 minor2(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[13], m_[14], m_[15]); + tmat3 minor3(m_[1], m_[2], m_[3], m_[9], m_[10], m_[11], m_[5], m_[6], m_[7]); + + tmat3 minor4(m_[4], m_[6], m_[7], m_[12], m_[14], m_[15], m_[8], m_[10], m_[11]); tmat3 minor5(m_[0], m_[2], m_[3], m_[8], m_[10], m_[11], m_[12], m_[14], m_[15]); - tmat3 minor6(m_[0], m_[1], m_[3], m_[8], m_[9], m_[11], m_[12], m_[13], m_[15]); - tmat3 minor7(m_[0], m_[1], m_[2], m_[8], m_[9], m_[10], m_[12], m_[13], m_[14]); - tmat3 minor8(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[13], m_[14], m_[15]); - tmat3 minor9(m_[0], m_[2], m_[3], m_[4], m_[6], m_[7], m_[12], m_[14], m_[15]); + tmat3 minor6(m_[0], m_[2], m_[3], m_[12], m_[14], m_[15], m_[4], m_[6], m_[7]); + tmat3 minor7(m_[0], m_[2], m_[3], m_[4], m_[6], m_[7], m_[8], m_[10], m_[11]); + + tmat3 minor8(m_[4], m_[5], m_[7], m_[8], m_[9], m_[11], m_[12], m_[13], m_[15]); + tmat3 minor9(m_[0], m_[1], m_[3], m_[12], m_[13], m_[15], m_[8], m_[9], m_[11]); tmat3 minor10(m_[0], m_[1], m_[3], m_[4], m_[5], m_[7], m_[12], m_[13], m_[15]); - tmat3 minor11(m_[0], m_[1], m_[2], m_[4], m_[5], m_[6], m_[12], m_[13], m_[14]); - tmat3 minor12(m_[1], m_[2], m_[3], m_[5], m_[6], m_[7], m_[9], m_[10], m_[11]); - tmat3 minor13(m_[0], m_[2], m_[3], m_[4], m_[6], m_[7], m_[8], m_[10], m_[11]); - tmat3 minor14(m_[0], m_[1], m_[3], m_[4], m_[5], m_[7], m_[8], m_[9], m_[11]); + tmat3 minor11(m_[0], m_[1], m_[3], m_[8], m_[9], m_[11], m_[4], m_[5], m_[7]); + + tmat3 minor12(m_[4], m_[5], m_[6], m_[12], m_[13], m_[14], m_[8], m_[9], m_[10]); + tmat3 minor13(m_[0], m_[1], m_[2], m_[8], m_[9], m_[10], m_[12], m_[13], m_[14]); + tmat3 minor14(m_[0], m_[1], m_[2], m_[12], m_[13], m_[14], m_[4], m_[5], m_[6]); tmat3 minor15(m_[0], m_[1], m_[2], m_[4], m_[5], m_[6], m_[8], m_[9], m_[10]); m_[0] = minor0.determinant() / d; m_[1] = minor1.determinant() / d; @@ -693,48 +731,73 @@ static const int precision(6); // row 0 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][2]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0][3]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[0]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[4]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[8]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[12]; std::cout << " |" << std::endl; // row 1 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][2]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1][3]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[1]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[5]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[9]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[13]; std::cout << " |" << std::endl; // row 2 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][2]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2][3]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[2]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[6]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[10]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[14]; std::cout << " |" << std::endl; // row 3 std::cout << "| "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3][0]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3][1]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3][2]; - std::cout << " "; - std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3][3]; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[3]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[7]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[11]; + std::cout << " "; + std::cout << std::fixed << std::showpoint << std::setprecision(precision) << m_[15]; std::cout << " |" << std::endl; } operator const T*() const { return &m_[0];} + bool operator==(const tmat4& rhs) const + { + return m_[0] == rhs.m_[0] && + m_[1] == rhs.m_[1] && + m_[2] == rhs.m_[2] && + m_[3] == rhs.m_[3] && + m_[4] == rhs.m_[4] && + m_[5] == rhs.m_[5] && + m_[6] == rhs.m_[6] && + m_[7] == rhs.m_[7] && + m_[8] == rhs.m_[8] && + m_[9] == rhs.m_[9] && + m_[10] == rhs.m_[10] && + m_[11] == rhs.m_[11] && + m_[12] == rhs.m_[12] && + m_[13] == rhs.m_[13] && + m_[14] == rhs.m_[14] && + m_[15] == rhs.m_[15]; + } + + bool operator!=(const tmat4& rhs) const + { + return !(*this == rhs); + } + tmat4& operator=(const tmat4& rhs) { if (this != &rhs)