I need a Java library to help me solve multi-times equations - java

I just started using Java to simulate solar panel's performance, however the single diode model requires lots of calculations:
basically, each function is a function of the other, all of them of the variable of Rs, a.
two equations are available from f, f1.
how can i get the value of Rs, a.
I can extract Rs, a from the equations and take it to the equation of f, f1.
however, the equation will be of great difficulty to solve, as it has exp(), multi-times of the variable and ln()..
is there any library to use to calculate the value of variables by given constraint?
the equations are basically like this:
v = 3.5a
ff0= 5/v+ln(5/v+0.7)/(5/v+1)
ffs = ff0*(1-Rs*1.1)+(Rs*8)^2
....
...
..
f: 9=constant1-ffs*((exp(v-4/Rs))-1)-(15+8*Rs)*a
f1: 9=15*(ffs*.....)
thank you.

You haven't concisely characterized the kind of equations that you want to solve, but the examples look rather hairy. If they are solvable (symbolically) then a commercial package like Mathematica is probably a good bet.
But I think you'd probably be better off asking this kind of question on the
http://math.stackexchange.com/
where real mathematicians hang out. If nothing else they can characterize your equations for you and tell you if they are likely to be solvable.

Related

cannot write variables on the summation of cplex java

I have been working on a mathematical model for 3 months. I have a constraint like this:
Where x and y are decision variables, p is a parameter.
I wrote my mathematical model using Java, I tried a lot of things but all of them doesn’t work correctly or gives error.
Would you please help me?

Is there a way to compare two methods by function rather than value? [duplicate]

This question already has answers here:
Is finding the equivalence of two functions undecidable?
(9 answers)
Closed 6 years ago.
Is there a way to compare if two methods are equivalent by function (i.e. they do the same thing) rather than equivalent by value (i.e. all of the code in the method is the same) ?
For example these two methods are coded differently, but perform the same function.
public int doIt(int a, int b) {
a = a + 1;
b = b + 1;
return a + b;
}
public int doIt2(int z, int x) {
int total = z + x + 2;
return total;
}
I was looking for a way to do this in Eclipse, but am interested if this is even possible beyond a trivial method.
The only way to be 100% is to mathematically prove it
There are ways:
1- Theorem proving
2- Model Checking
and etc
Although these approaches can be very hard, sometime it might take days to prove it even for trivial programs and even days to produce the adequate abstraction level.
There are some heuristic approaches but obviously they are not 100% accurate (heuristic)
A simple heuristic approach would be to try both methods for 1000 inputs and see if the results are the same
EDIT:
here is a list of Model Checker I found on Wikipedia. I haven't used any of them, they may not be exactly what you are looking for.
https://en.wikipedia.org/wiki/List_of_model_checking_tools
Ignoring side effects, 2 functions will be functionally equivalent if for the same input, they produce the same output.
This will only work for pure code though. There's no way I know of to monitor for side effects in general since the side effects a function carries out could be anything.
Note, there wouldn't be a way to completely verify this without testing every possible input. If the input is just a limited Enum, that might be easy. If it's 2 integers though for example, the total number of combinations would be huge.
In general, the purpose of refactoring is to have a function behave the same before and after it is refactored. Developers generally do this by creating extensive unit tests, testing both normal, edge, and exception cases.
In the OP's two functions to be compared, doIt and doIt2, they might usually return the same answer, given any integer inputs a and b. Unit testing would demonstrate this.
But what if a or b were the largest integer that Java could store, MAX_VALUE?
What if there were a side effect from a=a+1?
In these cases, the two functions may appear similar on the surface, but yield different results.

Java input from text file with an unusual format

So I realize this text format may not be very unusual. However, I've been trying many ideas to read this correctly into the objects needed and know there has to be a better way. Here is what the file looks like:
S S n
B 1 E 2
B N n
C 2 F 3
C N n
D 2 GA 4
D N n
GA 1
E N n
B 1 F 3 H 6
F N n
I 3 GA 3 C 1
A N g
H B b
U 2 GB 2 F 1
I N n
GA 2 GB 2
GB N g
So the first line of each pair is the name of the node S, whether its a starting node N/s then whether its a goal node g/n
The second line is the children of node S, and their distance weight.
For example, Node S has child node B with a distance of 1, and child node E with a distance of 2
I'm working with two object types, Nodes, and Edges.
Example below. (Constructors ommitted)
Does anyone have tips on how to read this type of input efficiently?
public class Edge {
public String start;
public String end;
public int weight;
public class Node {
public String name;
public boolean start = false;
public boolean goal = false;
public ArrayList<Edge> adjecentNodes = new ArrayList<Edge>();
Actually your question is almost too broad and unspecific, but I am in the mood to give you some starting points. But please understand that you could easily fill several hours of computer science lectures on this topic; and that is not going to happen here.
First, you have to clarify some requirements (for yourself; or together with the folks working on this project):
Do you really need to focus on efficient reading/building of your graph? I rather doubt that: building the graph happens once; but the computations that you probably do later on may run for a much longer time. So one primary focus should be on designing that object/class model that allows you to efficiently solve the problems that you want to solve on that graph! For example: it might be beneficial to already sort edges by distance/weight when creating the graph. Or maybe not. Depends on later use cases! And even when you are talking about huge files that need efficient processing ... that still means: you are talking about huge graphs; so all the more reason to find a good model for that graph.
Your description of the file is not clear. For example: is this a (un)directed graph? Meaning - can you travel on any edge in both direction? And sorry, I didn't get what a "goal" node is supposed to be. (I guess you have directed edges that go one way only, as that would explain those rows in the example where nodes do not have any children). Of course, sometimes requirements become clear in that moment when you start writing real code. But this here is really about concepts/data/algorithms. So the earlier you answer all such questions, the better for you.
Secondly, a suggestion in which order to do things:
As said, clarify all your requirements. Spend some serious time just thinking about the properties of the graphs you are dealing with; and what problems you later have to solve on them.
Start coding; ideally you use TDD/unit testing here. Because all of the things you are going to do can be nicely sliced into small work packages, and each one could be tested with unit-tests. Do not write all your code first, to then, after 2 days running your first experiments! The first thing you code: your Node/Edge classes ... because you want to play around with things like: what arguments do my constructors need? how can I make my classes immutable (data is pushed in by constructors only)? Do I want distance to be a property of my Edge; or can I just go with Node objects (and represent edges as Map<Node, Integer> --- meaning each node just knows its neighbors and the distance to get there!)
Then, when you are convinced that that Node/Edge fit your problem, then you start writing code that takes strings and builds Node/Edges out of those strings.
You also went to spent some time on writing good dump methods; ideally you call graph.dump() ... and that produces a string matching your input format (makes a nice test later on: reading + dumping should result in identical files!)
Then, when "building from strings" works ... then you write the few lines of "file parsing code" that uses some BufferedReader, FileReader, Scanner, Whatever mechanism to dissect your input file into strings ... which you then feed into the methods you created above for step 3.
And, seriously: if this is for school/learning:
Try to talk to your peers often. Have them look at what you are doing. Not too early, but also not too late.
Really, seriously: consider throwing away stuff; and starting from scratch again. For each of the steps above, or after going through the whole sequence. It is an incredible experience to do that; because typically, you come up with new, different, interesting ideas each time you do that.
And finally, some specific hints:
It is tempting to use "public" (writable) fields like start/end/... in your classes. But: consider not doing that. Try to hide as much of the internals of your classes. Because that will make it easier (or possible!) later on to change one part of your program without the need to change anything else, too.
Example:
class Edge {
private final int distance;
private final Node target;
public Edge(int distance, Node target) {
this.distance = distance; this.target = target;
}
...
This creates an immutable object - you can't change its core internal properties after the object was created. That is very often helpful.
Then: override methods like toString(), equals(), hashCode() in your classes; and use them. For example, toString() can be used to create a nice, human-readable dump of a node.
Finally: if you liked all of that, consider remembering my user id; and when you reach enough reputation to "upvote", come back and upvote ;-)

java.math.BigDecimal.scale() equivalent for double

I've got a matrix of values like the one below that I need to scale. I've been looking around for an inbuilt function if there is one that could do this for me. I haven't found one & so have ended up writing code to do the scaling using the below formula
scaledMatrix = (Matrix - MeanMatrix)/Standard Deviation
This code is a bit buggy & I'm working on correcting it. While I do that, I happened to bump on java.math.BigDecimal.scale() & did look up an equivalent for double as the matrix I have is double type numbers
If someone could please help me with details on
1) If there is an inbuilt function that accepts matrix of values & returns me the scaled matrix
2) `java.math.BigDecimal.scale()` equivalent for `double` type data
Any help would be much appreciated please.
The BigDecimal.scale() method does not do what you seem to think it is doing. A BigDecimal value is stored as a * 10^b (where ^ denotes exponentiation). The BigDecimal.scale() method basically returns the b part of that.
I do not know of a similar method for double values, nor do I know of a method which performs the function you need. Since you put apache-commons in the tags, I suggest you look into Apache Commons's extensive statistical library.

Correct AST for Parsing Algebra

What is the correct abstract syntax tree for representing algebra? I have tried way too many setups, and constantly been rewriting the syntax tree, and all of my configurations end up forgetting something important (e.g. fractions not being supported). Currently my configurations for equations and expressions seem to be fine. Expressions simply consist of an array of terms, each with a positive/negative sign, and a coefficient. That's where the trouble comes in. What exactly is a term? Wikipedia helps some, and even has an example AST for a couple of terms. However, for practical purposes I'm trying to keep everything closer to the concepts we use when we learn algebra, rather than breaking it down into nothing but variables and operators. It appears that just about anything can be contained in a term: terms can contain fractions (which contain expressions), sub-terms, sub-expressions, and regular variables, each of them having their own exponents.
Currently my configuration is something like this:
Term
|
-----------------------------------------------------------------
| | | | |
Coefficient ArrayList of ArrayList of ArrayList of ArrayList of
| sub-expressions powers of fractions powers of
| sub-expressions* (may contain fractions*
--------------- variables)
| |
integer/decimal fraction
(no variables)
*Expressions/fractions don't have exponents on their own, but may have one outside sometimes (e.g. 2(x+3)^3).
NOTE: For the sake of simplicity the diagram leaves out an ArrayList of variables (and one for roots), an an ArrayList of their respective exponents, all contained by the term.
NOTE 2: In case it's not clear, the diagram doesn't show inheritance. It's showing members of the Term class.
This seems rather sloppy to me, and might not scale well with the project when things get more complex. Is a term really supposed to be this kind of soup? I have a feeling yet another thing should be included in term, but I can't think of what it would be. Although I've been strugling with this for some months, I haven't taken the discipline to just stop and really work it out, which I should have done before starting.
Am I making a mistake in making nearly everything fit in a term? If so, what should I be doing instead? If not, is it really supposed to be this... ugly/non-intuitive? Part of my feeling that this must be wrong is due to the fact that that almost no one thinks of an algebraic term this way.
Example term: 2.3x(2/3)^4(√23)((x+6)/(x-6)) (overly complex, I know, but it contains everything mentioned above).
My real question: What is the correct syntax structure for the the term, the heart and soul of algebra?

Categories

Resources