Skip to content

Commit

Permalink
Добавил работу с уравнениями, зачатки solve.
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-karpovich committed May 11, 2014
1 parent 8f454cc commit 8f59795
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/by/bsu/coursework/structures/Commutation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package by.bsu.coursework.structures;

import java.util.LinkedList;

/**
*
* @author th13f
*/
public class Commutation {
private Variable from;
private LinkedList<EquationPart> to;

public Commutation(Variable from) {
this.from = from;
this.to = new LinkedList<>();
}

public void setTo(LinkedList<EquationPart> to) {
this.to = to;
}

public void addTo(EquationPart v){
to.add(v);
}

@Override
public String toString() {
String result=from.toString()+"->";
for (EquationPart p:to)
result+=p.toString();
return result;
}

public Variable getFrom() {
return from;
}

public LinkedList<EquationPart> getTo() {
return to;
}
}
55 changes: 55 additions & 0 deletions src/main/java/by/bsu/coursework/structures/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package by.bsu.coursework.structures;

/**
*
* @author th13f
*/
public class Constant implements EquationPart{
private double value;

public Constant(double value) {
this.value = value;
}

@Override
public String toString() {
String result;
if (value>=0)
result="+";
else
result="-";
return result+value;
}

@Override
public EquationPart inversed() {
return new Constant(-this.value);
}

public double getValue(){
return value;
}

public void setValue(double value){
this.value = value;
}

@Override
public String getType() {
return "Constant";
}

@Override
public double getCoefficient(){
return this.value;
}

@Override
public void setCoefficient(double coeff){
this.value = coeff;
}
}
121 changes: 121 additions & 0 deletions src/main/java/by/bsu/coursework/structures/Equation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package by.bsu.coursework.structures;

import java.util.LinkedList;

/**
*
* @author th13f
*/
public class Equation {
private LinkedList<EquationPart> leftPart;
private LinkedList<EquationPart> rightPart;

public Equation() {
leftPart=new LinkedList<>();
rightPart=new LinkedList<>();
}

public void addToLeft(EquationPart var){
leftPart.add(var);
}

public void addToRight(EquationPart var){
leftPart.add(var);
}

@Override
public String toString() {
String result = "";
for(EquationPart v:leftPart)
result+=v.toString();
result+=" = ";
for(EquationPart v:rightPart)
result+=v.toString();
return result;
}

public void simplify(LinkedList<EquationPart> to){
Constant tempConstant = new Constant(0);
for (int i=0; i<to.size();){
if (to.get(i).getType().equals("Constant")){
tempConstant.setValue(tempConstant.getValue()+((Constant)to.get(i)).getValue());
to.remove(i);
}
else
i++;
}
to.add(tempConstant);
}

public Commutation solve(Variable what){
Commutation result = new Commutation(what);

LinkedList<EquationPart> to = (LinkedList)rightPart.clone();
for (EquationPart var:leftPart){
to.add(var.inversed());
}
simplify(to);
result.setTo(to);

return result;
}

public void insert(Commutation what){
for (int i=0; i<leftPart.size();){
if (what.getFrom().getName().equals(what.getFrom().getName()) &&
what.getFrom().getSubIndex().equals(what.getFrom().getSubIndex()) &&
what.getFrom().getTopIndex().equals(what.getFrom().getTopIndex())){
double alpha = leftPart.get(i).getCoefficient();
leftPart.remove(leftPart.get(i));
for (EquationPart var:what.getTo()){
switch (var.getType()) {
case "Variable":
leftPart.add(new Variable(
((Variable)var).getName(),
((Variable)var).getSubIndex(),
((Variable)var).getTopIndex(),
((Variable)var).getCoefficient()*alpha));
break;
case "Constant":
leftPart.add(new Constant(
((Variable)var).getCoefficient()*alpha));
break;
}
}
}
else{
i++;
}
}
for (int i=0; i<rightPart.size();){
if (what.getFrom().getName().equals(what.getFrom().getName()) &&
what.getFrom().getSubIndex().equals(what.getFrom().getSubIndex()) &&
what.getFrom().getTopIndex().equals(what.getFrom().getTopIndex())){
double alpha = rightPart.get(i).getCoefficient();
rightPart.remove(rightPart.get(i));
for (EquationPart var:what.getTo()){
switch (var.getType()) {
case "Variable":
rightPart.add(new Variable(
((Variable)var).getName(),
((Variable)var).getSubIndex(),
((Variable)var).getTopIndex(),
((Variable)var).getCoefficient()*alpha));
break;
case "Constant":
rightPart.add(new Constant(
((Variable)var).getCoefficient()*alpha));
break;
}
}
}
else{
i++;
}
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/by/bsu/coursework/structures/EquationPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package by.bsu.coursework.structures;

/**
*
* @author th13f
*/
public interface EquationPart {
@Override
public String toString();

public EquationPart inversed();

public String getType();

public double getCoefficient();
public void setCoefficient(double coeff);
}
68 changes: 68 additions & 0 deletions src/main/java/by/bsu/coursework/structures/Variable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package by.bsu.coursework.structures;

/**
*
* @author th13f
*/
public class Variable implements EquationPart{
private String name;
private String subIndex;
private String topIndex;
private double coefficient;

public Variable(String name, String subIndex, String topIndex, double coefficient) {
this.name = name;
this.subIndex = subIndex;
this.topIndex = topIndex;
this.coefficient = coefficient;
}

@Override
public String toString() {
String result;
if (coefficient>=0){
result = "+";
}
else{
result = "-";
}

return result+Math.abs(coefficient)+name+topIndex+"_"+subIndex;
}

@Override
public EquationPart inversed() {
return new Variable(name, subIndex, topIndex, -coefficient);
}

@Override
public String getType() {
return "Variable";
}

public String getName() {
return name;
}

public String getSubIndex() {
return subIndex;
}

public String getTopIndex() {
return topIndex;
}

@Override
public double getCoefficient() {
return coefficient;
}

@Override
public void setCoefficient(double coeff){
this.coefficient = coeff;
}
}

0 comments on commit 8f59795

Please sign in to comment.