Add some matrix classes in math\matrix package and standard libraries
This commit is contained in:
256
src/stdlib/StdArrayIO.java
Normal file
256
src/stdlib/StdArrayIO.java
Normal file
@@ -0,0 +1,256 @@
|
||||
package stdlib;
|
||||
|
||||
/*************************************************************************
|
||||
* Compilation: javac StdArrayIO.java
|
||||
* Execution: java StdArrayIO < input.txt
|
||||
*
|
||||
* A library for reading in 1D and 2D arrays of integers, doubles,
|
||||
* and booleans from standard input and printing them out to
|
||||
* standard output.
|
||||
*
|
||||
* % more tinyDouble1D.txt
|
||||
* 4
|
||||
* .000 .246 .222 -.032
|
||||
*
|
||||
* % more tinyDouble2D.txt
|
||||
* 4 3
|
||||
* .000 .270 .000
|
||||
* .246 .224 -.036
|
||||
* .222 .176 .0893
|
||||
* -.032 .739 .270
|
||||
*
|
||||
* % more tinyBoolean2D.txt
|
||||
* 4 3
|
||||
* 1 1 0
|
||||
* 0 0 0
|
||||
* 0 1 1
|
||||
* 1 1 1
|
||||
*
|
||||
* % cat tinyDouble1D.txt tinyDouble2D.txt tinyBoolean2D.txt | java StdArrayIO
|
||||
* 4
|
||||
* 0.00000 0.24600 0.22200 -0.03200
|
||||
*
|
||||
* 4 3
|
||||
* 0.00000 0.27000 0.00000
|
||||
* 0.24600 0.22400 -0.03600
|
||||
* 0.22200 0.17600 0.08930
|
||||
* 0.03200 0.73900 0.27000
|
||||
*
|
||||
* 4 3
|
||||
* 1 1 0
|
||||
* 0 0 0
|
||||
* 0 1 1
|
||||
* 1 1 1
|
||||
*
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
/**
|
||||
* <i>Standard array IO</i>. This class provides methods for reading
|
||||
* in 1D and 2D arrays from standard input and printing out to
|
||||
* standard output.
|
||||
* <p>
|
||||
* For additional documentation, see
|
||||
* <a href="http://introcs.cs.princeton.edu/22libary">Section 2.2</a> of
|
||||
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i>
|
||||
* by Robert Sedgewick and Kevin Wayne.
|
||||
*
|
||||
* @author Robert Sedgewick
|
||||
* @author Kevin Wayne
|
||||
*/
|
||||
public class StdArrayIO {
|
||||
|
||||
// it doesn't make sense to instantiate this class
|
||||
private StdArrayIO() { }
|
||||
|
||||
/**
|
||||
* Read in and return an array of doubles from standard input.
|
||||
*/
|
||||
public static double[] readDouble1D() {
|
||||
int N = StdIn.readInt();
|
||||
double[] a = new double[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
a[i] = StdIn.readDouble();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an array of doubles to standard output.
|
||||
*/
|
||||
public static void print(double[] a) {
|
||||
int N = a.length;
|
||||
StdOut.println(N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
StdOut.printf("%9.5f ", a[i]);
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read in and return an M-by-N array of doubles from standard input.
|
||||
*/
|
||||
public static double[][] readDouble2D() {
|
||||
int M = StdIn.readInt();
|
||||
int N = StdIn.readInt();
|
||||
double[][] a = new double[M][N];
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
a[i][j] = StdIn.readDouble();
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the M-by-N array of doubles to standard output.
|
||||
*/
|
||||
public static void print(double[][] a) {
|
||||
int M = a.length;
|
||||
int N = a[0].length;
|
||||
StdOut.println(M + " " + N);
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
StdOut.printf("%9.5f ", a[i][j]);
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read in and return an array of ints from standard input.
|
||||
*/
|
||||
public static int[] readInt1D() {
|
||||
int N = StdIn.readInt();
|
||||
int[] a = new int[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
a[i] = StdIn.readInt();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an array of ints to standard output.
|
||||
*/
|
||||
public static void print(int[] a) {
|
||||
int N = a.length;
|
||||
StdOut.println(N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
StdOut.printf("%9d ", a[i]);
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read in and return an M-by-N array of ints from standard input.
|
||||
*/
|
||||
public static int[][] readInt2D() {
|
||||
int M = StdIn.readInt();
|
||||
int N = StdIn.readInt();
|
||||
int[][] a = new int[M][N];
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
a[i][j] = StdIn.readInt();
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the M-by-N array of ints to standard output.
|
||||
*/
|
||||
public static void print(int[][] a) {
|
||||
int M = a.length;
|
||||
int N = a[0].length;
|
||||
StdOut.println(M + " " + N);
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
StdOut.printf("%9d ", a[i][j]);
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read in and return an array of booleans from standard input.
|
||||
*/
|
||||
public static boolean[] readBoolean1D() {
|
||||
int N = StdIn.readInt();
|
||||
boolean[] a = new boolean[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
a[i] = StdIn.readBoolean();
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an array of booleans to standard output.
|
||||
*/
|
||||
public static void print(boolean[] a) {
|
||||
int N = a.length;
|
||||
StdOut.println(N);
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (a[i]) StdOut.print("1 ");
|
||||
else StdOut.print("0 ");
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read in and return an M-by-N array of booleans from standard input.
|
||||
*/
|
||||
public static boolean[][] readBoolean2D() {
|
||||
int M = StdIn.readInt();
|
||||
int N = StdIn.readInt();
|
||||
boolean[][] a = new boolean[M][N];
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
a[i][j] = StdIn.readBoolean();
|
||||
}
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the M-by-N array of booleans to standard output.
|
||||
*/
|
||||
public static void print(boolean[][] a) {
|
||||
int M = a.length;
|
||||
int N = a[0].length;
|
||||
StdOut.println(M + " " + N);
|
||||
for (int i = 0; i < M; i++) {
|
||||
for (int j = 0; j < N; j++) {
|
||||
if (a[i][j]) StdOut.print("1 ");
|
||||
else StdOut.print("0 ");
|
||||
}
|
||||
StdOut.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test client.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// read and print an array of doubles
|
||||
double[] a = StdArrayIO.readDouble1D();
|
||||
StdArrayIO.print(a);
|
||||
StdOut.println();
|
||||
|
||||
// read and print a matrix of doubles
|
||||
double[][] b = StdArrayIO.readDouble2D();
|
||||
StdArrayIO.print(b);
|
||||
StdOut.println();
|
||||
|
||||
// read and print a matrix of doubles
|
||||
boolean[][] d = StdArrayIO.readBoolean2D();
|
||||
StdArrayIO.print(d);
|
||||
StdOut.println();
|
||||
}
|
||||
|
||||
}
|
||||
210
src/stdlib/StdIn.java
Normal file
210
src/stdlib/StdIn.java
Normal file
@@ -0,0 +1,210 @@
|
||||
package stdlib;
|
||||
|
||||
/*************************************************************************
|
||||
* Compilation: javac StdIn.java
|
||||
* Execution: java StdIn
|
||||
*
|
||||
* Reads in data of various types from standard input.
|
||||
*
|
||||
*************************************************************************/
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.util.Locale;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* <i>Standard input</i>. This class provides methods for reading strings
|
||||
* and numbers from standard input.
|
||||
* <p>
|
||||
* The Locale used is: language = English, country = US. This is consistent
|
||||
* with the formatting conventions with Java floating-point literals,
|
||||
* command-line arguments (via <tt>Double.parseDouble()</tt>)
|
||||
* and standard output (via <tt>System.out.print()</tt>). It ensures that
|
||||
* standard input works with the input files used in the textbook.
|
||||
* <p>
|
||||
* For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
|
||||
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
|
||||
*/
|
||||
public final class StdIn {
|
||||
|
||||
// assume Unicode UTF-8 encoding
|
||||
private static String charsetName = "UTF-8";
|
||||
|
||||
// assume language = English, country = US for consistency with System.out.
|
||||
private static Locale usLocale = new Locale("en", "US");
|
||||
|
||||
// the scanner object
|
||||
private static Scanner scanner = new Scanner(new BufferedInputStream(System.in), charsetName);
|
||||
|
||||
// static initializer
|
||||
static { scanner.useLocale(usLocale); }
|
||||
|
||||
// singleton pattern - can't instantiate
|
||||
private StdIn() { }
|
||||
|
||||
|
||||
/**
|
||||
* Is there only whitespace left on standard input?
|
||||
*/
|
||||
public static boolean isEmpty() {
|
||||
return !scanner.hasNext();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next string from standard input
|
||||
*/
|
||||
public static String readString() {
|
||||
return scanner.next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next int from standard input
|
||||
*/
|
||||
public static int readInt() {
|
||||
return scanner.nextInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next double from standard input
|
||||
*/
|
||||
public static double readDouble() {
|
||||
return scanner.nextDouble();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next float from standard input
|
||||
*/
|
||||
public static float readFloat() {
|
||||
return scanner.nextFloat();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next short from standard input
|
||||
*/
|
||||
public static short readShort() {
|
||||
return scanner.nextShort();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next long from standard input
|
||||
*/
|
||||
public static long readLong() {
|
||||
return scanner.nextLong();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next byte from standard input
|
||||
*/
|
||||
public static byte readByte() {
|
||||
return scanner.nextByte();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next boolean from standard input, allowing "true" or "1" for true,
|
||||
* and "false" or "0" for false
|
||||
*/
|
||||
public static boolean readBoolean() {
|
||||
String s = readString();
|
||||
if (s.equalsIgnoreCase("true")) return true;
|
||||
if (s.equalsIgnoreCase("false")) return false;
|
||||
if (s.equals("1")) return true;
|
||||
if (s.equals("0")) return false;
|
||||
throw new java.util.InputMismatchException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Does standard input have a next line?
|
||||
*/
|
||||
public static boolean hasNextLine() {
|
||||
return scanner.hasNextLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return rest of line from standard input
|
||||
*/
|
||||
public static String readLine() {
|
||||
return scanner.nextLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next char from standard input
|
||||
*/
|
||||
// a complete hack and inefficient - email me if you have a better
|
||||
public static char readChar() {
|
||||
// (?s) for DOTALL mode so . matches a line termination character
|
||||
// 1 says look only one character ahead
|
||||
// consider precompiling the pattern
|
||||
String s = scanner.findWithinHorizon("(?s).", 1);
|
||||
return s.charAt(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return rest of input from standard input
|
||||
*/
|
||||
public static String readAll() {
|
||||
if (!scanner.hasNextLine()) return null;
|
||||
|
||||
// reference: http://weblogs.java.net/blog/pat/archive/2004/10/stupid_scanner_1.html
|
||||
return scanner.useDelimiter("\\A").next();
|
||||
}
|
||||
|
||||
/**
|
||||
* Read rest of input as array of ints
|
||||
*/
|
||||
public static int[] readInts() {
|
||||
String[] fields = readAll().trim().split("\\s+");
|
||||
int[] vals = new int[fields.length];
|
||||
for (int i = 0; i < fields.length; i++)
|
||||
vals[i] = Integer.parseInt(fields[i]);
|
||||
return vals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read rest of input as array of doubles
|
||||
*/
|
||||
public static double[] readDoubles() {
|
||||
String[] fields = readAll().trim().split("\\s+");
|
||||
double[] vals = new double[fields.length];
|
||||
for (int i = 0; i < fields.length; i++)
|
||||
vals[i] = Double.parseDouble(fields[i]);
|
||||
return vals;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read rest of input as array of strings
|
||||
*/
|
||||
public static String[] readStrings() {
|
||||
String[] fields = readAll().trim().split("\\s+");
|
||||
return fields;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Unit test
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
System.out.println("Type a string: ");
|
||||
String s = StdIn.readString();
|
||||
System.out.println("Your string was: " + s);
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Type an int: ");
|
||||
int a = StdIn.readInt();
|
||||
System.out.println("Your int was: " + a);
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Type a boolean: ");
|
||||
boolean b = StdIn.readBoolean();
|
||||
System.out.println("Your boolean was: " + b);
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Type a double: ");
|
||||
double c = StdIn.readDouble();
|
||||
System.out.println("Your double was: " + c);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
230
src/stdlib/StdOut.java
Normal file
230
src/stdlib/StdOut.java
Normal file
@@ -0,0 +1,230 @@
|
||||
package stdlib;
|
||||
|
||||
/*************************************************************************
|
||||
* Compilation: javac StdOut.java
|
||||
* Execution: java StdOut
|
||||
*
|
||||
* Writes data of various types to standard output.
|
||||
*
|
||||
*************************************************************************/
|
||||
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* <i>Standard output</i>. This class provides methods for writing strings
|
||||
* and numbers to standard output.
|
||||
* <p>
|
||||
* For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
|
||||
* <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
|
||||
*/
|
||||
public final class StdOut {
|
||||
|
||||
// force Unicode UTF-8 encoding; otherwise it's system dependent
|
||||
private static final String UTF8 = "UTF-8";
|
||||
|
||||
// assume language = English, country = US for consistency with StdIn
|
||||
private static final Locale US_LOCALE = new Locale("en", "US");
|
||||
|
||||
// send output here
|
||||
private static PrintWriter out;
|
||||
|
||||
// this is called before invoking any methods
|
||||
static {
|
||||
try {
|
||||
out = new PrintWriter(new OutputStreamWriter(System.out, UTF8), true);
|
||||
}
|
||||
catch (UnsupportedEncodingException e) { System.out.println(e); }
|
||||
}
|
||||
|
||||
// singleton pattern - can't instantiate
|
||||
private StdOut() { }
|
||||
|
||||
// close the output stream (not required)
|
||||
/**
|
||||
* Close standard output.
|
||||
*/
|
||||
public static void close() {
|
||||
out.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate the current line by printing the line separator string.
|
||||
*/
|
||||
public static void println() {
|
||||
out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an object to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(Object x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a boolean to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(boolean x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a char to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(char x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a double to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(double x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a float to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(float x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an int to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(int x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a long to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(long x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a short to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(short x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a byte to standard output and then terminate the line.
|
||||
*/
|
||||
public static void println(byte x) {
|
||||
out.println(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush standard output.
|
||||
*/
|
||||
public static void print() {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an Object to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(Object x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a boolean to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(boolean x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a char to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(char x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a double to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(double x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a float to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(float x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print an int to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(int x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a long to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(long x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a short to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(short x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a byte to standard output and flush standard output.
|
||||
*/
|
||||
public static void print(byte x) {
|
||||
out.print(x);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a formatted string to standard output using the specified
|
||||
* format string and arguments, and flush standard output.
|
||||
*/
|
||||
public static void printf(String format, Object... args) {
|
||||
out.printf(US_LOCALE, format, args);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a formatted string to standard output using the specified
|
||||
* locale, format string, and arguments, and flush standard output.
|
||||
*/
|
||||
public static void printf(Locale locale, String format, Object... args) {
|
||||
out.printf(locale, format, args);
|
||||
out.flush();
|
||||
}
|
||||
|
||||
// This method is just here to test the class
|
||||
public static void main(String[] args) {
|
||||
|
||||
// write to stdout
|
||||
StdOut.println("Test");
|
||||
StdOut.println(17);
|
||||
StdOut.println(true);
|
||||
StdOut.printf("%.6f\n", 1.0/7.0);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user