Commit e3077b08 authored by Dr. Martin Goik's avatar Dr. Martin Goik

Exercise exponential, minor javadoc comments, small bugs

parent ac837b53
......@@ -23,12 +23,13 @@ POMDIRS= $(shell find P -name pom.xml|sed -e s/pom.xml//g)
BUILDBASE=$(BUILDROOT)/$(SCRIPTBASENAME)
BUILDNAVI=$(BUILDBASE)/navi/$(PLUGINID)
BUILDNONAVI=$(BUILDBASE)/nonavi/$(PLUGINID)
JAVADOCDESTDIR=${BUILDNAVI}/Ref/api
#
#Rule to build javadoc from pom.xml file
#
%/jdoc : %
cd $*; mvn javadoc:javadoc
mkdir -p ${JAVADOCDESTDIR}/$*
cd $*; mvn -DjavadocDestdir=${JAVADOCDESTDIR}/$* javadoc:javadoc
all:${BUILDNAVI}/index.html ${BUILDNAVI}/printversion.pdf jdoc
......@@ -58,6 +59,6 @@ install::${BUILDNAVI}/index.html
ssh infocenter2@www.mi.hdm-stuttgart.de /usr/share/hdm/indexupdate.pl
clean:
rm -rf ${BUILDBASE} ${SCRIPTBASENAME}.convert.xml ${SCRIPTBASENAME}.convert.answer.* resources *~
rm -rf ${BUILDBASE} ${SCRIPTBASENAME}.convert.xml ${SCRIPTBASENAME}.answer.* resources *~
cd Ref/Fig;$(MAKE) clean
#end
/.settings
/target
/.classpath
/.project
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.de.sd1</groupId>
<artifactId>math</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>math</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javadocDestdir>~/tmp</javadocDestdir>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<linksource>true</linksource>
<!-- Setting destDir interferes with taglet class -->
<additionalJOptions>
<additionalJOption>-d ${javadocDestdir}</additionalJOption>
</additionalJOptions>
<taglets>
<taglet>
<tagletClass>de.hdm_stuttgart.de.sd1.taglet.HtmlExtensionTaglet</tagletClass>
</taglet>
</taglets>
<tagletpath>../../../../../../../ws/eclipse/HtmlExtensionTaglet/target/classes</tagletpath>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include> **/*.properties</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package de.hdm_stuttgart.de.sd1.math;
/**
* Testing {@link Math#exp(double)}in
*
*/
public class Driver {
/**
* @param args Unused
*/
public static void main(String[] args) {
Math.setSeriesLimit(6);
double byPowerSeries = Math.exp(1.);
System.out.println("e=" + byPowerSeries + ", diff=" + (byPowerSeries - java.lang.Math.exp(1.)));
byPowerSeries = Math.exp(2.);
System.out.println("e=" + byPowerSeries + ", diff=" + (byPowerSeries - java.lang.Math.exp(2.)));
byPowerSeries = Math.exp(3.);
System.out.println("e=" + byPowerSeries + ", diff=" + (byPowerSeries - java.lang.Math.exp(3.)));
}
}
package de.hdm_stuttgart.de.sd1.math;
/**
* Hint: Embedded formulas can only be viewed correctly when
* using Firefox or Safari web browsers.
*
* This class is used to illustrate the implementation of a subset of functions from
* {@link java.lang.Math} by means of their corresponding power series.
*
*/
public class Math {
static int seriesLimit = 1;
/**
*
* @param seriesLimit The last term's index of a power series to be included,
* {@link }}.
*/
public static void setSeriesLimit(int seriesLimit) {
Math.seriesLimit = seriesLimit;
}
/**
* Approximating the natural exponential function by a finite
* number of terms from the corresponding power series:
*
* {@html.extend
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
<mtable>
<mtr>
<mtd>
<mrow>
<msup>
<mi>e</mi>
<mi>x</mi>
</msup>
<mo>=</mo>
<mrow>
<mi>1</mi>
<mo>+</mo>
<mfrac>
<msup>
<mi>x</mi>
<mi>1</mi>
</msup>
<mi>1!</mi>
</mfrac>
<mo>+</mo>
<mfrac>
<msup>
<mi>x</mi>
<mi>2</mi>
</msup>
<mi>2!</mi>
</mfrac>
<mo>+</mo>
<mfrac>
<msup>
<mi>x</mi>
<mi>3</mi>
</msup>
<mi>3!</mi>
</mfrac>
<mo>+</mo>
<mn>...</mn>
<mo>+</mo>
<mfrac>
<msup>
<mi>x</mi>
<mi>seriesLimit</mi>
</msup>
<mrow>
<mi>seriesLimit</mi>
<mi>!</mi>
</mrow>
</mfrac>
</mrow>
</mrow>
</mtd>
</mtr>
<mtr>
<mtd>
<mrow>
<mo>=</mo>
<mrow>
<munderover>
<mo>∑</mo>
<mrow>
<mi>i</mi>
<mo>=</mo>
<mi>0</mi>
</mrow>
<mi mathvariant="normal">seriesLimit</mi>
</munderover>
<mfrac>
<msup>
<mi>x</mi>
<mi>i</mi>
</msup>
<mrow>
<mi>i</mi>
<mi>!</mi>
</mrow>
</mfrac>
</mrow>
</mrow>
</mtd>
</mtr>
</mtable>
</math>}
*
* A power series implementation has to be finite since an
* infinite number of terms requires infinite execution time.
*
* The number of terms to be considered can be set by {@link #setSeriesLimit(int)}}
*
* @param x The exponential's argument as in {@html.extend
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup>
<mi>e</mi>
<mi>x</mi>
</msup>
</mrow>
</math>}
* @return The value {@html.extend
<math display="inline" xmlns="http://www.w3.org/1998/Math/MathML">
<mrow>
<msup>
<mi>e</mi>
<mi>x</mi>
</msup>
</mrow>
</math>} itself.
*/
public static double exp(double x) {
double currentTerm = 1., // the first (i == 0) term x^0/0!
sum = currentTerm; // initialize to the power series' first term
for (int i = 1; i <= seriesLimit; i++) { // i = 0 has already been completed.
currentTerm *= x / i;
sum += currentTerm;
}
return sum;
}
}
package de.hdm_stuttgart.de.sd1.math;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Testing exponential implementation
*
*/
public class ExpTest extends TestCase {
/**
* @return the suite of tests being tested
*/
public static Test suite() {
final TestSuite testsuite = new TestSuite("Testing exponential");
testsuite.addTestSuite(ExpTest.class);
return testsuite; }
/**
* Sums and products of fractions
*/
public void testApp() {
Math.setSeriesLimit(8);
assertTrue(java.lang.Math.abs(Math.exp(1) - java.lang.Math.E) < 1.E-4); // Mind limited computational accuracy!
}
}
/.settings
/.classpath
/.project
/target
......@@ -3,11 +3,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.hdm-stuttgart.de.sd1</groupId>
<artifactId>loop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>rounding</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>loop</name>
<name>rounding</name>
<url>http://maven.apache.org</url>
<properties>
......
package de.hdm_stuttgart.de.sd1.leap;
/**
* Leap year calculations
*
*/
public class LeapYear {
/**
* @param args unused
*/
public static void main(String[] args) {
System.out.println("Is 1800 a leap year? " + isLeapYear(1800));
System.out.println("Is 2000 a leap year? " + isLeapYear(2000));
......
package de.hdm_stuttgart.de.sd1.leap;
/**
* Leap year calculations
*
*/
public class LeapYearCompact {
/**
* @param args unused
*/
public static void main(String[] args) {
System.out.println("Is 1800 a leap year? " + isLeapYear(1800));
System.out.println("Is 2000 a leap year? " + isLeapYear(2000));
......
......@@ -6,6 +6,10 @@ package de.hdm_stuttgart.de.sd1.rounding;
*
*/
public class MathTable {
/**
* @param args unused
*/
public static void main(String[] args) {
System.out.println(" x | sin(x)");
......
package de.hdm_stuttgart.de.sd1.sum;
/**
* Summing up integer values.
*
*/
public class Summing {
/**
* @param args unused
*/
public static void main(String[] args) {
System.out.println("1 + 2 + ... + 150" + "=" + getSum(150));
}
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment