Skip to content

Commit

Permalink
[jml] delete JUnit dependencies - use Google formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
axkr committed Dec 26, 2020
1 parent 0f2c8ee commit 9cb4b48
Show file tree
Hide file tree
Showing 271 changed files with 47,728 additions and 42,686 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
*/
package de.tilman_neumann.jml;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.apache.log4j.Logger;
Expand All @@ -23,58 +21,67 @@

/**
* Binary search in bottom-up sorted integer arrays.
*
* @author Tilman Neumann
*/
public class BinarySearch {
private static final Logger LOG = Logger.getLogger(BinarySearch.class);
private static final boolean DEBUG = false;

/**
* Find the insert position for x into array given that array is sorted bottom-up.
*
* More precisely:
* If array[maxIndex-1] > x, return the index of the first entry of array[0].. array[maxIndex-1] greater than x.
* If array[maxIndex-1] <= x, return maxIndex.
*
* @param array
* @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
* @param x
* @return the insert position
*/
public int getInsertPosition(int[] array, int maxIndex, int x) {
if (maxIndex<=0 || array[maxIndex-1] <= x) return maxIndex;
int left = 0;
int right = maxIndex-1;
int median;
do {
median = (left+right)>>1; // floor
if (array[median] <= x) {
// the tested element was smaller or equal -> the right insert position must be higher than median
left = median + 1;
} else {
// the tested element was bigger-> median could be the right insert position or too big
right = median;
}
} while (left!=right);
if (DEBUG) {
if (left>0) assertTrue(array[left-1] <= x);
assertTrue(x < array[left]);
}
return left;
}

private static void testSingleArg(BinarySearch bs, int[] array, int maxIndex, int x) {
int index = bs.getInsertPosition(array, maxIndex, x);
LOG.info("index of '" + x + "' in " + Arrays.toString(array) + " (maxIndex=" + maxIndex + ") = " + index);
}

public static void main(String[] args) {
ConfigUtil.initProject();
int[] array = new int[] {1,2,3,7,10,10,11};
BinarySearch bs = new BinarySearch();
testSingleArg(bs, array, 4, 3);
testSingleArg(bs, array, 7, 10);
testSingleArg(bs, array, 7, 11);
testSingleArg(bs, array, 7, 20);
}
private static final Logger LOG = Logger.getLogger(BinarySearch.class);
private static final boolean DEBUG = false;

/**
* Find the insert position for x into array given that array is sorted bottom-up.
*
* <p>More precisely: If array[maxIndex-1] > x, return the index of the first entry of array[0]..
* array[maxIndex-1] greater than x. If array[maxIndex-1] <= x, return maxIndex.
*
* @param array
* @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
* @param x
* @return the insert position
*/
public int getInsertPosition(int[] array, int maxIndex, int x) {
if (maxIndex <= 0 || array[maxIndex - 1] <= x) return maxIndex;
int left = 0;
int right = maxIndex - 1;
int median;
do {
median = (left + right) >> 1; // floor
if (array[median] <= x) {
// the tested element was smaller or equal -> the right insert position must be higher than
// median
left = median + 1;
} else {
// the tested element was bigger-> median could be the right insert position or too big
right = median;
}
} while (left != right);
// if (DEBUG) {
// if (left>0) assertTrue(array[left-1] <= x);
// assertTrue(x < array[left]);
// }
return left;
}

private static void testSingleArg(BinarySearch bs, int[] array, int maxIndex, int x) {
int index = bs.getInsertPosition(array, maxIndex, x);
LOG.info(
"index of '"
+ x
+ "' in "
+ Arrays.toString(array)
+ " (maxIndex="
+ maxIndex
+ ") = "
+ index);
}

public static void main(String[] args) {
ConfigUtil.initProject();
int[] array = new int[] {1, 2, 3, 7, 10, 10, 11};
BinarySearch bs = new BinarySearch();
testSingleArg(bs, array, 4, 3);
testSingleArg(bs, array, 7, 10);
testSingleArg(bs, array, 7, 11);
testSingleArg(bs, array, 7, 20);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,58 +25,62 @@

/**
* Computation of values of the Chebyshev polynomials.
*
* @author Tilman Neumann
*/
public class ChebyshevPolynomials {
private static final Logger LOG = Logger.getLogger(ChebyshevPolynomials.class);

/**
* Recurrent computation of Chebyshev polynomials of the first kind.
* @param n degree
* @param x argument
* @return T_n(x)
*/
public static BigDecimal ChebyshevT(int n, BigDecimal x) {
if (n==0) return F_1;
if (n==1) return x;
return F_2.multiply(x).multiply(ChebyshevT(n-1, x)).subtract(ChebyshevT(n-2, x));
}
private static final Logger LOG = Logger.getLogger(ChebyshevPolynomials.class);

/**
* Recurrent computation of Chebyshev polynomials of the first kind.
*
* @param n degree
* @param x argument
* @return T_n(x)
*/
public static BigDecimal ChebyshevT(int n, BigDecimal x) {
if (n == 0) return F_1;
if (n == 1) return x;
return F_2.multiply(x).multiply(ChebyshevT(n - 1, x)).subtract(ChebyshevT(n - 2, x));
}

/**
* Recurrent computation of Chebyshev polynomials of the second kind.
*
* @param n degree
* @param x argument
* @return U_n(x)
*/
public static BigDecimal ChebyshevU(int n, BigDecimal x) {
if (n == 0) return F_1;
if (n == 1) return F_2.multiply(x);
return F_2.multiply(x).multiply(ChebyshevU(n - 1, x)).subtract(ChebyshevU(n - 2, x));
}

/**
* Recurrent computation of Chebyshev polynomials of the second kind.
* @param n degree
* @param x argument
* @return U_n(x)
*/
public static BigDecimal ChebyshevU(int n, BigDecimal x) {
if (n==0) return F_1;
if (n==1) return F_2.multiply(x);
return F_2.multiply(x).multiply(ChebyshevU(n-1, x)).subtract(ChebyshevU(n-2, x));
}

/**
* Test.
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();
for (int n=0; n<=10; n++) {
String nStr = "n=" + n + ": T_" + n + "(x) = ";
for (BigInteger x=I_0; x.compareTo(I_10)<0; x=x.add(I_1)) {
BigDecimal T_n_of_x = ChebyshevT(n, new BigDecimal(x));
nStr += T_n_of_x + ", ";
}
nStr = nStr.substring(0, nStr.length()-2);
LOG.info(nStr);
}
for (int n=0; n<=10; n++) {
String nStr = "n=" + n + ": U_" + n + "(x) = ";
for (BigInteger x=I_0; x.compareTo(I_10)<0; x=x.add(I_1)) {
BigDecimal U_n_of_x = ChebyshevU(n, new BigDecimal(x));
nStr += U_n_of_x + ", ";
}
nStr = nStr.substring(0, nStr.length()-2);
LOG.info(nStr);
}
}
/**
* Test.
*
* @param args ignored
*/
public static void main(String[] args) {
ConfigUtil.initProject();
for (int n = 0; n <= 10; n++) {
String nStr = "n=" + n + ": T_" + n + "(x) = ";
for (BigInteger x = I_0; x.compareTo(I_10) < 0; x = x.add(I_1)) {
BigDecimal T_n_of_x = ChebyshevT(n, new BigDecimal(x));
nStr += T_n_of_x + ", ";
}
nStr = nStr.substring(0, nStr.length() - 2);
LOG.info(nStr);
}
for (int n = 0; n <= 10; n++) {
String nStr = "n=" + n + ": U_" + n + "(x) = ";
for (BigInteger x = I_0; x.compareTo(I_10) < 0; x = x.add(I_1)) {
BigDecimal U_n_of_x = ChebyshevU(n, new BigDecimal(x));
nStr += U_n_of_x + ", ";
}
nStr = nStr.substring(0, nStr.length() - 2);
LOG.info(nStr);
}
}
}
Loading

0 comments on commit 9cb4b48

Please sign in to comment.