Sorry for not keeping up with your previous posts. I was traveling last week and forgot about this.
$num = Compute("42/9");
Paraphrasing from Davide Cervone about this: Compute() calls Function() on the string "42/9", after first setting the context flags reduceConstants, reduceConstantFunctions, and showExtraParens to 0. Then if the formula is constant (which it is in this case), the formula is evaluated to get a Real, which is already something with a floating point value. Then Formula() is once again called on the original string "42/9" (this time with the reductions turned off), and this is used to produce the string and TeX versions for the correct answer.
So $num is a Real. Its value method gives 4.666... Its string and TeX methods give 4.66667. And its correct_ans and correct_ans_latex_string properties (properties, not methods) are respectively '42/9' and '\frac{42}{9}'.
So the things you want (those fractions in the last two properties I mentioned) are just not what you get when you use $num as a string. Instead you get the result of the string method: 4.66667.
What you really want is either:
A. Explicitly make $num be a Formula() object, and use the context flag reduceConstants => 0. So:
Context()->flags->set(reduceConstants => 0);
$num = Formula("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
B. Use the Fraction context, which under default settings is also going to reduce your "42/9" to "14/3":
loadMacros("contextFraction.pl");
Context("Fraction");
$num = Compute("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
However, both of these ways of doing it cause your "sqrt3" to be a decimal. This is because $func is the result of a Formula() times a Formula(), or a Fraction() times a Formula(). The result is a Formula() that did not get the special treatment that Compute() uses for setting those special string properties. So now what you probably want is to stop things like "sqrt(3)" from being executed. That's a function applied to a constant, and for that there is the flag with the slightly confusing name reduceConstantFunctions:
Context()->flags->set(reduceConstants => 0, reduceConstantFunctions => 0);
$num = Formula("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
loadMacros("contextFraction.pl");
Context("Fraction")->flags->set(reduceConstantFunctions => 0);
$num = Compute("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
$num = Compute("42/9");
Paraphrasing from Davide Cervone about this: Compute() calls Function() on the string "42/9", after first setting the context flags reduceConstants, reduceConstantFunctions, and showExtraParens to 0. Then if the formula is constant (which it is in this case), the formula is evaluated to get a Real, which is already something with a floating point value. Then Formula() is once again called on the original string "42/9" (this time with the reductions turned off), and this is used to produce the string and TeX versions for the correct answer.
So $num is a Real. Its value method gives 4.666... Its string and TeX methods give 4.66667. And its correct_ans and correct_ans_latex_string properties (properties, not methods) are respectively '42/9' and '\frac{42}{9}'.
So the things you want (those fractions in the last two properties I mentioned) are just not what you get when you use $num as a string. Instead you get the result of the string method: 4.66667.
What you really want is either:
A. Explicitly make $num be a Formula() object, and use the context flag reduceConstants => 0. So:
Context()->flags->set(reduceConstants => 0);
$num = Formula("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
B. Use the Fraction context, which under default settings is also going to reduce your "42/9" to "14/3":
loadMacros("contextFraction.pl");
Context("Fraction");
$num = Compute("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
However, both of these ways of doing it cause your "sqrt3" to be a decimal. This is because $func is the result of a Formula() times a Formula(), or a Fraction() times a Formula(). The result is a Formula() that did not get the special treatment that Compute() uses for setting those special string properties. So now what you probably want is to stop things like "sqrt(3)" from being executed. That's a function applied to a constant, and for that there is the flag with the slightly confusing name reduceConstantFunctions:
Context()->flags->set(reduceConstants => 0, reduceConstantFunctions => 0);
$num = Formula("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;
loadMacros("contextFraction.pl");
Context("Fraction")->flags->set(reduceConstantFunctions => 0);
$num = Compute("42/9");
$func_sub = Compute("cos(sqrt3x)");
$func = $num*$func_sub;