-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Units Library #10
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.team4159.lib.logging; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public class CSVWriter { | ||
private FileWriter writer; | ||
|
||
public CSVWriter(File file) { | ||
try { | ||
writer = new FileWriter(file); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void write(Object... columns) { | ||
try { | ||
for (Object column : columns) { | ||
writer.append(column.toString()); | ||
writer.append(","); | ||
} | ||
writer.append("\n"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void close() { | ||
try { | ||
writer.flush(); | ||
writer.close(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.team4159.lib.math; | ||
|
||
public class Epsilon { | ||
public static final double kEpsilon = 1E-9; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
class Ampere extends Unit { | ||
@Override | ||
public String symbol() { | ||
return "A"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
class Kilogram extends Unit { | ||
@Override | ||
public String symbol() { | ||
return "kg"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
class Meter extends Unit { | ||
@Override | ||
public String symbol() { | ||
return "m"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
import java.util.*; | ||
|
||
class MultiUnit extends Unit { | ||
private Map<Unit, Integer> units; | ||
|
||
MultiUnit(Map<Unit, Integer> units) { | ||
this.units = units; | ||
} | ||
|
||
@Override | ||
public Unit mult(Unit other) { | ||
Map<Unit, Integer> new_units = getUnits(); | ||
if (other instanceof MultiUnit) { | ||
for (Map.Entry<Unit, Integer> unit_entry: ((MultiUnit) other).getUnits().entrySet()) { | ||
new_units.merge(unit_entry.getKey(), unit_entry.getValue(), Integer::sum); | ||
if (new_units.get(unit_entry.getKey()) == 0) new_units.remove(unit_entry.getKey()); | ||
} | ||
} else { | ||
new_units.merge(other, 1, Integer::sum); | ||
} | ||
return new MultiUnit(new_units); | ||
} | ||
|
||
@Override | ||
public Unit inv() { | ||
HashMap<Unit, Integer> inverse_units = new HashMap<>(); | ||
for (Map.Entry<Unit, Integer> unit_entry: getUnits().entrySet()) { | ||
units.put(unit_entry.getKey(), -unit_entry.getValue()); | ||
} | ||
return new MultiUnit(inverse_units); | ||
} | ||
|
||
@Override | ||
public String symbol() { | ||
ArrayList<Map.Entry<Unit, Integer>> sorted_units = new ArrayList<>(getUnits().entrySet()); | ||
sorted_units.sort((a, b) -> { | ||
int compare = a.getValue() - b.getValue(); | ||
if (compare == 0) compare = b.getKey().symbol().compareTo(a.getKey().symbol()); | ||
return compare; | ||
}); | ||
StringBuilder name = new StringBuilder(); | ||
boolean slashed = false; | ||
for (int i = sorted_units.size() - 1; i >= 0; i--) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need some newlines in the mehtod to organize the code better |
||
Map.Entry<Unit, Integer> unit_entry = sorted_units.get(i); | ||
if (!slashed && unit_entry.getValue() < 0) { | ||
if (i == sorted_units.size() - 1) name.append(1); | ||
name.append("/"); | ||
slashed = true; | ||
} | ||
int order = Math.abs(unit_entry.getValue()); | ||
if (order > 0) name.append(unit_entry.getKey().symbol()); | ||
if (order > 1) name.append("^").append(order); | ||
} | ||
return name.toString(); | ||
} | ||
|
||
public Map<Unit, Integer> getUnits() { | ||
return new HashMap<>(units); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof MultiUnit)) return false; | ||
return units.equals(((MultiUnit) obj).units); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
public class Quantity { | ||
private Unit unit; | ||
private double amount; | ||
|
||
public Quantity(double amount, Unit unit) { | ||
this.amount = amount; | ||
this.unit = unit; | ||
} | ||
|
||
public Quantity mult(Quantity other) { | ||
return new Quantity(amount * other.amount, unit.mult(other.unit)); | ||
} | ||
|
||
public Quantity div(Quantity other) { | ||
return mult(new Quantity(1.0 / other.amount, other.unit.inv())); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return amount + " " + unit.symbol(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does Java automatically convert doubles to string when concatenating with string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Quantity)) return false; | ||
Quantity other = (Quantity) obj; | ||
return this.amount == other.amount && this.unit.equals(other.unit); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
class Second extends Unit { | ||
@Override | ||
public String symbol() { | ||
return "s"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
import java.util.Map; | ||
|
||
public class Unit { | ||
public String symbol() { | ||
return "ul"; | ||
} | ||
|
||
public Unit mult(Unit other) { | ||
if (other instanceof MultiUnit) { | ||
return other.mult(this); | ||
} else { | ||
return new MultiUnit(Map.of(this, 1, other, 1)); | ||
} | ||
} | ||
|
||
public Unit div(Unit other) { | ||
return mult(other.inv()); | ||
} | ||
|
||
public Unit inv() { | ||
return new MultiUnit(Map.of(this, -1)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return symbol(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (!(obj instanceof Unit)) return false; | ||
return symbol().equals(((Unit) obj).symbol()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.team4159.lib.math.units; | ||
|
||
/* | ||
* Inspired by https://github.com/team5419/fault/tree/master/src/main/kotlin/org/team5419/fault/math/units | ||
*/ | ||
|
||
import org.team4159.lib.math.units.derivatives.Acceleration; | ||
import org.team4159.lib.math.units.derivatives.Jerk; | ||
|
||
public class Units { | ||
public static final Unit UNIT = new Unit(); | ||
|
||
public static final Unit SECOND = new Second(); | ||
public static final Unit KILOGRAM = new Kilogram(); | ||
public static final Unit METER = new Meter(); | ||
public static final Unit AMPERE = new Ampere(); | ||
|
||
// derived | ||
public static final Unit NEWTON = Acceleration.of(KILOGRAM.mult(METER)); | ||
public static final Unit VOLT = Jerk.of(KILOGRAM.mult(METER).mult(METER).div(AMPERE)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.team4159.lib.math.units.derivatives; | ||
|
||
import org.team4159.lib.math.units.Unit; | ||
import static org.team4159.lib.math.units.Units.*; | ||
|
||
public class Acceleration { | ||
public static Unit of(Unit unit) { | ||
return unit.div(SECOND).div(SECOND); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.team4159.lib.math.units.derivatives; | ||
|
||
import org.team4159.lib.math.units.Unit; | ||
import static org.team4159.lib.math.units.Units.*; | ||
|
||
public class Jerk { | ||
public static Unit of(Unit unit) { | ||
return unit.div(SECOND).div(SECOND).div(SECOND); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.team4159.lib.math.units.derivatives; | ||
|
||
import org.team4159.lib.math.units.Unit; | ||
import static org.team4159.lib.math.units.Units.*; | ||
|
||
public class Velocity { | ||
public static Unit of(Unit unit) { | ||
return unit.div(SECOND); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seperate this into another function and pass that function into the sort