Source Code Cross Referenced for MatrixUtilsTest.java in  » Science » Apache-commons-math-1.1 » org » apache » commons » math » linear » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Apache commons math 1.1 » org.apache.commons.math.linear 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004 The Apache Software Foundation.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.apache.commons.math.linear;
017:
018:        import java.math.BigDecimal;
019:        import junit.framework.Test;
020:        import junit.framework.TestCase;
021:        import junit.framework.TestSuite;
022:
023:        /**
024:         * Test cases for the {@link MatrixUtils} class.
025:         *
026:         * @version $Revision: 171229 $ $Date: 2005-05-21 09:29:19 -0700 (Sat, 21 May 2005) $
027:         */
028:
029:        public final class MatrixUtilsTest extends TestCase {
030:
031:            protected double[][] testData = { { 1d, 2d, 3d }, { 2d, 5d, 3d },
032:                    { 1d, 0d, 8d } };
033:            protected double[][] nullMatrix = null;
034:            protected double[] row = { 1, 2, 3 };
035:            protected BigDecimal[] bigRow = { new BigDecimal(1),
036:                    new BigDecimal(2), new BigDecimal(3) };
037:            protected String[] stringRow = { "1", "2", "3" };
038:            protected double[][] rowMatrix = { { 1, 2, 3 } };
039:            protected BigDecimal[][] bigRowMatrix = { { new BigDecimal(1),
040:                    new BigDecimal(2), new BigDecimal(3) } };
041:            protected String[][] stringRowMatrix = { { "1", "2", "3" } };
042:            protected double[] col = { 0, 4, 6 };
043:            protected BigDecimal[] bigCol = { new BigDecimal(0),
044:                    new BigDecimal(4), new BigDecimal(6) };
045:            protected String[] stringCol = { "0", "4", "6" };
046:            protected double[] nullDoubleArray = null;
047:            protected double[][] colMatrix = { { 0 }, { 4 }, { 6 } };
048:            protected BigDecimal[][] bigColMatrix = { { new BigDecimal(0) },
049:                    { new BigDecimal(4) }, { new BigDecimal(6) } };
050:            protected String[][] stringColMatrix = { { "0" }, { "4" }, { "6" } };
051:
052:            public MatrixUtilsTest(String name) {
053:                super (name);
054:            }
055:
056:            public void setUp() {
057:            }
058:
059:            public static Test suite() {
060:                TestSuite suite = new TestSuite(MatrixUtilsTest.class);
061:                suite.setName("MatrixUtils Tests");
062:                return suite;
063:            }
064:
065:            public void testCreateRealMatrix() {
066:                assertEquals(new RealMatrixImpl(testData), MatrixUtils
067:                        .createRealMatrix(testData));
068:                try {
069:                    MatrixUtils.createRealMatrix(new double[][] { { 1 },
070:                            { 1, 2 } }); // ragged
071:                    fail("Expecting IllegalArgumentException");
072:                } catch (IllegalArgumentException ex) {
073:                    // expected
074:                }
075:                try {
076:                    MatrixUtils.createRealMatrix(new double[][] { {}, {} }); // no columns
077:                    fail("Expecting IllegalArgumentException");
078:                } catch (IllegalArgumentException ex) {
079:                    // expected
080:                }
081:                try {
082:                    MatrixUtils.createRealMatrix(null); // null
083:                    fail("Expecting NullPointerException");
084:                } catch (NullPointerException ex) {
085:                    // expected
086:                }
087:            }
088:
089:            public void testCreateBigMatrix() {
090:                assertEquals(new BigMatrixImpl(testData), MatrixUtils
091:                        .createBigMatrix(testData));
092:                assertEquals(new BigMatrixImpl(bigColMatrix), MatrixUtils
093:                        .createBigMatrix(bigColMatrix));
094:                assertEquals(new BigMatrixImpl(stringColMatrix), MatrixUtils
095:                        .createBigMatrix(stringColMatrix));
096:                try {
097:                    MatrixUtils.createBigMatrix(new double[][] { { 1 },
098:                            { 1, 2 } }); // ragged
099:                    fail("Expecting IllegalArgumentException");
100:                } catch (IllegalArgumentException ex) {
101:                    // expected
102:                }
103:                try {
104:                    MatrixUtils.createBigMatrix(new double[][] { {}, {} }); // no columns
105:                    fail("Expecting IllegalArgumentException");
106:                } catch (IllegalArgumentException ex) {
107:                    // expected
108:                }
109:                try {
110:                    MatrixUtils.createBigMatrix(nullMatrix); // null
111:                    fail("Expecting NullPointerException");
112:                } catch (NullPointerException ex) {
113:                    // expected
114:                }
115:            }
116:
117:            public void testCreateRowRealMatrix() {
118:                assertEquals((RealMatrixImpl) MatrixUtils
119:                        .createRowRealMatrix(row),
120:                        new RealMatrixImpl(rowMatrix));
121:                try {
122:                    MatrixUtils.createRowRealMatrix(new double[] {}); // empty
123:                    fail("Expecting IllegalArgumentException");
124:                } catch (IllegalArgumentException ex) {
125:                    // expected
126:                }
127:                try {
128:                    MatrixUtils.createRowRealMatrix(null); // null
129:                    fail("Expecting NullPointerException");
130:                } catch (NullPointerException ex) {
131:                    // expected
132:                }
133:            }
134:
135:            public void testCreateRowBigMatrix() {
136:                assertEquals((BigMatrixImpl) MatrixUtils
137:                        .createRowBigMatrix(row), new BigMatrixImpl(rowMatrix));
138:                assertEquals((BigMatrixImpl) MatrixUtils
139:                        .createRowBigMatrix(bigRow), new BigMatrixImpl(
140:                        bigRowMatrix));
141:                assertEquals((BigMatrixImpl) MatrixUtils
142:                        .createRowBigMatrix(stringRow), new BigMatrixImpl(
143:                        stringRowMatrix));
144:                try {
145:                    MatrixUtils.createRowBigMatrix(new double[] {}); // empty
146:                    fail("Expecting IllegalArgumentException");
147:                } catch (IllegalArgumentException ex) {
148:                    // expected
149:                }
150:                try {
151:                    MatrixUtils.createRowBigMatrix(nullDoubleArray); // null
152:                    fail("Expecting NullPointerException");
153:                } catch (NullPointerException ex) {
154:                    // expected
155:                }
156:            }
157:
158:            public void testCreateColumnRealMatrix() {
159:                assertEquals((RealMatrixImpl) MatrixUtils
160:                        .createColumnRealMatrix(col), new RealMatrixImpl(
161:                        colMatrix));
162:                try {
163:                    MatrixUtils.createColumnRealMatrix(new double[] {}); // empty
164:                    fail("Expecting IllegalArgumentException");
165:                } catch (IllegalArgumentException ex) {
166:                    // expected
167:                }
168:                try {
169:                    MatrixUtils.createColumnRealMatrix(null); // null
170:                    fail("Expecting NullPointerException");
171:                } catch (NullPointerException ex) {
172:                    // expected
173:                }
174:            }
175:
176:            public void testCreateColumnBigMatrix() {
177:                assertEquals((BigMatrixImpl) MatrixUtils
178:                        .createColumnBigMatrix(col), new BigMatrixImpl(
179:                        colMatrix));
180:                assertEquals((BigMatrixImpl) MatrixUtils
181:                        .createColumnBigMatrix(bigCol), new BigMatrixImpl(
182:                        bigColMatrix));
183:                assertEquals((BigMatrixImpl) MatrixUtils
184:                        .createColumnBigMatrix(stringCol), new BigMatrixImpl(
185:                        stringColMatrix));
186:
187:                try {
188:                    MatrixUtils.createColumnBigMatrix(new double[] {}); // empty
189:                    fail("Expecting IllegalArgumentException");
190:                } catch (IllegalArgumentException ex) {
191:                    // expected
192:                }
193:                try {
194:                    MatrixUtils.createColumnBigMatrix(nullDoubleArray); // null
195:                    fail("Expecting NullPointerException");
196:                } catch (NullPointerException ex) {
197:                    // expected
198:                }
199:            }
200:
201:            /**
202:             * Verifies that the matrix is an identity matrix
203:             */
204:            protected void checkIdentityMatrix(RealMatrix m) {
205:                for (int i = 0; i < m.getRowDimension(); i++) {
206:                    for (int j = 0; j < m.getColumnDimension(); j++) {
207:                        if (i == j) {
208:                            assertEquals(m.getEntry(i, j), 1d, 0);
209:                        } else {
210:                            assertEquals(m.getEntry(i, j), 0d, 0);
211:                        }
212:                    }
213:                }
214:            }
215:
216:            public void testCreateIdentityMatrix() {
217:                checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(3));
218:                checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(2));
219:                checkIdentityMatrix(MatrixUtils.createRealIdentityMatrix(1));
220:                try {
221:                    MatrixUtils.createRealIdentityMatrix(0);
222:                } catch (IllegalArgumentException ex) {
223:                    // expected
224:                }
225:            }
226:
227:            /**
228:             * Verifies that the matrix is an identity matrix
229:             */
230:            protected void checkIdentityBigMatrix(BigMatrix m) {
231:                for (int i = 0; i < m.getRowDimension(); i++) {
232:                    for (int j = 0; j < m.getColumnDimension(); j++) {
233:                        if (i == j) {
234:                            assertEquals(m.getEntry(i, j), BigMatrixImpl.ONE);
235:                        } else {
236:                            assertEquals(m.getEntry(i, j), BigMatrixImpl.ZERO);
237:                        }
238:                    }
239:                }
240:            }
241:
242:            public void testCreateBigIdentityMatrix() {
243:                checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(3));
244:                checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(2));
245:                checkIdentityBigMatrix(MatrixUtils.createBigIdentityMatrix(1));
246:                try {
247:                    MatrixUtils.createRealIdentityMatrix(0);
248:                } catch (IllegalArgumentException ex) {
249:                    // expected
250:                }
251:            }
252:
253:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.