10 #define MAX_TEX_WIDTH 110
11 #define MAX_TEX_HEIGHT 50
13 //char* get_tex_flag_address();
17 int out_index
, out_length
;
19 static int char_count
, last_char
;
21 char *power_str
= "^";
24 int* get_tex_flag_address()
39 int old_tex_flag
= tex_flag
;
41 int width
, height
, baseline
;
42 char* edit_line
= (char*)Console_GetEditLine();
47 TeX_sizeComplex(edit_line
, &width
, &height
, &baseline
);
48 /*if(width > MAX_TEX_WIDTH || height > MAX_TEX_HEIGHT) {
50 Console_Clear_EditLine();
54 tex_flag
= old_tex_flag
;
71 if (sign_of_term(car(p
)) == '-')
76 if (sign_of_term(car(p
)) == '+')
90 if (sign_of_term(p
) == '-')
99 if (car(p
) == symbol(MULTIPLY
) && isnum(cadr(p
)) && lessp(cadr(p
), zero
))
101 else if (isnum(p
) && lessp(p
, zero
))
118 // count numerators and denominators
126 if (isrational(p2
)) {
146 if (is_denominator(p2
))
154 if(tex_flag
) print_str("\\frac{");
162 if (isrational(car(p1
)))
170 if (is_denominator(p2
))
174 print_multiply_sign();
183 if(tex_flag
) print_str("}{");
199 if (isrational(car(p1
)))
209 if (is_denominator(p2
)) {
211 print_multiply_sign();
221 if(!tex_flag
&& d
> 1) //if (d > 1)
224 if(tex_flag
) print_str("}");
234 int sqrt_factor_number
= 0, non_sqrt_factor_number
= 0, i
= 0;
236 if (car(p
) == symbol(MULTIPLY
) && any_denominators(p
)) {
241 if (car(p
) == symbol(MULTIPLY
)) {
246 if (isminusone(car(p
))) {
253 p_sqrt
= p
; //temp buffer
254 sqrt_factor_number
= 0;
255 non_sqrt_factor_number
= 0;
257 //Get the number of factor which are square root of something
258 while(iscons(p_sqrt
) || isnum(p_sqrt
)){
259 if(caar(p_sqrt
) == symbol(POWER
) && equalq(caddar(p
), 1, 2)) sqrt_factor_number
++;
260 else non_sqrt_factor_number
++;
261 p_sqrt
= cdr(p_sqrt
);
266 if(sqrt_factor_number
> 1) {
267 //First, print the factor wich are not square root of something
268 if(non_sqrt_factor_number
) {
269 //Print the first wich is not square root of something
270 while (iscons(p_sqrt
) || isnum(p_sqrt
)) {
271 if(!(caar(p_sqrt
) == symbol(POWER
) && equalq(caddar(p
), 1, 2))) {
272 print_factor(cdar(p_sqrt
));
275 else p_sqrt
= cdr(p_sqrt
);
278 p_sqrt
= cdr(p_sqrt
);
280 //Print the other ones
281 while ((iscons(p_sqrt
) || isnum(p_sqrt
)) && i
< non_sqrt_factor_number
) {
282 if(!(caar(p_sqrt
) == symbol(POWER
) && equalq(caddar(p
), 1, 2))){
283 print_multiply_sign();
284 print_factor(cdar(p_sqrt
));
285 p_sqrt
= cdr(p_sqrt
);
294 print_str("\\sqrt{");
296 while (iscons(p_sqrt
) || isnum(p_sqrt
)) {
297 if(caar(p_sqrt
) == symbol(POWER
) && equalq(caddar(p
), 1, 2)) {
298 print_expr(cadar(p_sqrt
));
302 p_sqrt
= cdr(p_sqrt
);
305 p_sqrt
= cdr(p_sqrt
);
307 while ((iscons(p_sqrt
) || isnum(p_sqrt
)) && i
< sqrt_factor_number
) {
308 if(caar(p_sqrt
) == symbol(POWER
) && equalq(caddar(p
), 1, 2)) {
309 print_multiply_sign();
310 print_expr(cadar(p_sqrt
));
312 p_sqrt
= cdr(p_sqrt
);
323 print_factor(car(p
));
326 print_multiply_sign();
327 print_factor(car(p
));
334 // prints stuff after the divide symbol "/"
336 // d is the number of denominators
342 print_denom(U
*p
, int d
)
349 // i.e. 1 / (2^(1/3))
351 if (d
== 1 && !isminusone(EXPO
))
355 if (tex_flag
&& equalq(EXPO
, -1, 2)) {
356 print_str("\\sqrt{");
364 if (isfraction(BASE
) || car(BASE
) == symbol(ADD
) || car(BASE
) == symbol(MULTIPLY
) || car(BASE
) == symbol(POWER
) || lessp(BASE
, zero
)) {
371 if (isminusone(EXPO
)) {
377 print_str(power_str
);
385 if (isfraction(EXPO
) || car(EXPO
) == symbol(ADD
) || car(EXPO
) == symbol(MULTIPLY
) || car(EXPO
) == symbol(POWER
)) {
418 if (isadd(p
) || car(p
) == symbol(MULTIPLY
)) {
425 if (car(p
) == symbol(POWER
)) {
427 if (cadr(p
) == symbol(E
)) {
433 print_expr(caddr(p
));
438 if (isminusone(caddr(p
))) {
440 if(tex_flag
) print_str("\\frac{1}{");
441 else print_str("1 / ");
445 if (iscons(cadr(p
))) {
452 if(tex_flag
) print_str("}");
457 if (tex_flag
&& equalq(caddr(p
), 1, 2)) {
458 print_str("\\sqrt{");
464 if (tex_flag
&& equalq(caddr(p
), -1, 2)) {
465 print_str("\\frac{1}{\\sqrt{");
473 if (isadd(cadr(p
)) || caadr(p
) == symbol(MULTIPLY
) || caadr(p
) == symbol(POWER
) || isnegativenumber(cadr(p
))) {
477 } else if (isnum(cadr(p
)) && (lessp(cadr(p
), zero
) || isfraction(cadr(p
)))) {
479 print_factor(cadr(p
));
482 print_factor(cadr(p
));
485 print_str(power_str
);
491 a
= iscons(caddr(p
));
492 b
= isfraction(caddr(p
));
493 c
= (isnum(caddr(p
)) && lessp(caddr(p
), zero
));
497 print_expr(caddr(p
));
500 print_factor(caddr(p
));
505 // if (car(p) == _list) {
509 // print_expr(car(p));
512 // while (iscons(p)) {
514 // print_expr(car(p));
521 if (car(p
) == symbol(INDEX
) && issymbol(cadr(p
))) {
522 print_index_function(p
);
526 if (car(p
) == symbol(FACTORIAL
)) {
527 print_factorial_function(p
);
532 //if (car(p) == symbol(FORMAL) && cadr(p)->k == SYM) {
533 // print_str(((struct symbol *) cadr(p))->name);
536 print_factor(car(p
));
555 if (p
== symbol(DERIVATIVE
))
557 else if (p
== symbol(E
))
559 print_str("\235(1)");
563 else if (p
== symbol(PI
))
567 Console_Output((const unsigned char *)"pi");
570 print_str(get_printname(p
));
574 print_index_function(U
*p
)
577 if (caar(p
) == symbol(ADD
) || caar(p
) == symbol(MULTIPLY
) || caar(p
) == symbol(POWER
) || caar(p
) == symbol(FACTORIAL
))
578 print_subexpr(car(p
));
596 print_factorial_function(U
*p
)
599 if (car(p
) == symbol(ADD
) || car(p
) == symbol(MULTIPLY
) || car(p
) == symbol(POWER
) || car(p
) == symbol(FACTORIAL
))
610 print_tensor_inner(p
, 0, &k
);
614 print_tensor_inner(U
*p
, int j
, int *k
)
618 for (i
= 0; i
< p
->u
.tensor
->dim
[j
]; i
++) {
619 if (j
+ 1 == p
->u
.tensor
->ndim
) {
620 print_expr(p
->u
.tensor
->elem
[*k
]);
623 print_tensor_inner(p
, j
+ 1, k
);
624 if (i
+ 1 < p
->u
.tensor
->dim
[j
]) {
646 // if (display_flag == 1)
653 print_function_definition(U
*p
)
655 print_str(get_printname(p
));
656 print_arg_list(cadr(get_binding(p
)));
658 print_expr(caddr(get_binding(p
)));
666 print_str(get_printname(car(p
)));
670 print_str(get_printname(car(p
)));
696 if (p
!= symbol(NIL
)) {
712 print_str(get_printname(p
));
715 print_str("<tensor>");
721 print_multiply_sign(void)
732 if (car(p
) == symbol(POWER
) && cadr(p
) != symbol(E
) && isnegativeterm(caddr(p
)))
738 // don't consider the leading fraction
740 // we want 2/3*a*b*c instead of 2*a*b*c/3
743 any_denominators(U
*p
)
747 // if (isfraction(car(p)))
751 if (is_denominator(q
))