**If you're not interested in the background information explaining my reasoning skip to the very bottom for the end of my question.
I have a set of polynomial equations i need to read into my program to perform unit tests of polynomials such as
so i need to be able to input a wide range of numbers to test the functions used in BRLCAD, though i am having trouble coming up with an efficient solution to read in numbers. My current approach:
Given the command line:
./unit_test sd 2 3 4 8 1 4 5
writing an algorithm to read a set of numbers in this format is cumbersome and quite confusing for others to understand, plus i have yet to implement two other polynomials to be read in as the answers to be compared with.(as this is an open source file, i would like my work to have some transparency).
if(strcmp(argv[1],"sd") == 0){
poly_eqn1.dgr = atoi(argv[2]);
/* start at argv[3], run degree count plus one times */
for(counter = 0; counter < (eqn1.dgr + 1); counter++)
poly_eqn1.cf[counter] = atof(argv[counter+3]);
poly_eqn2.dgr = atoi(argv[4 + poly_eqn1.dgr]);
/* start at end of degree one counter */
for(counter = 0; counter < (dgr2 + 1); counter++)
poly_eqn2.cf[counter] = atof(argv[counter+5+dgr]);
/* grab the answer from end of data */
return test_synthetic(//input proper data...);
}
Would using sscanf
be more efficent to read in my polynomial given a maximum degree is set? After writing the initial method i thought about using sscanf, but i'm unsure if using it in such a manner would be worth it as opposed to writing in two more polynomial read in's from above:
Given the maximum degree is four for a polynomial
./unit_test sd 2,0,0,2,4,5 1,0,0,0,2,3
If I were you, I would try hard to support your parameters in human readable format.
For example, it should be possible to parse input in following form:
./unit_test "(3*x^2+4*x+8)/(4*x+5)"
Granted, you may have to write lexical parser to understand this input, but it would be easiest to understand from user perspective.
You really need not worry about efficiency when simply processing the command line arguments. Do so in the simplest, most straightforward fashion appropriate for your task. Spend your time worrying about efficiency where it matter - in tight loops with the actual number crunching algorithms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With