AnnaAneta commited on
Commit
19c66ac
·
verified ·
1 Parent(s): 065ba89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +181 -4
app.py CHANGED
@@ -497,6 +497,7 @@ def parse_recurrence_equation(equation_text):
497
  """
498
  Analizuje rekurencję w postaci:
499
  a_0*T_0 = c_0
 
500
  a_n*T_n = b_n*T_{n-1} + c_n
501
  Zwraca: a_0, a_n, b_n, c_n, c_0, T_0, b_1, S_n (sympy + string), initials
502
 
@@ -512,8 +513,7 @@ def parse_recurrence_equation(equation_text):
512
  T = sp.Function("T")
513
 
514
  print(f"Symbol n created: {n}, id: {id(n)}")
515
-
516
- print("=== RAW INPUT ===")
517
  print(equation_text)
518
 
519
  # Znajdujemy równanie główne (a_n*T_n = b_n*T_{n-1} + c_n)
@@ -631,7 +631,184 @@ def parse_recurrence_equation(equation_text):
631
  "initials": inits
632
  }
633
 
634
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
 
636
 
637
  # --- Dopasowanie podobnych zadań ---
@@ -645,7 +822,7 @@ def find_similar(query, top_k=3):
645
  # --- Tryb wskazówek ---
646
  def hint_mode(user_problem, step_idx=0):
647
  try:
648
- coeffs = parse_recurrence_equation(user_problem)
649
  for k, v in coeffs.items():
650
  if hasattr(v, "__str__"):
651
  coeffs[k] = str(v)
 
497
  """
498
  Analizuje rekurencję w postaci:
499
  a_0*T_0 = c_0
500
+ [opcjonalnie] a_1*T_1 = b_1*T_0 + c_1
501
  a_n*T_n = b_n*T_{n-1} + c_n
502
  Zwraca: a_0, a_n, b_n, c_n, c_0, T_0, b_1, S_n (sympy + string), initials
503
 
 
513
  T = sp.Function("T")
514
 
515
  print(f"Symbol n created: {n}, id: {id(n)}")
516
+ print("=== RAW INPUT OLD===")
 
517
  print(equation_text)
518
 
519
  # Znajdujemy równanie główne (a_n*T_n = b_n*T_{n-1} + c_n)
 
631
  "initials": inits
632
  }
633
 
634
+ def parse_recurrence_equation_3(equation_text):
635
+ """
636
+ Analizuje rekurencję w postaci:
637
+ a_0*T_0 = c_0
638
+ [opcjonalnie] a_1*T_1 = b_1*T_0 + c_1
639
+ a_n*T_n = b_n*T_{n-1} + c_n
640
+ Zwraca: a_0, a_1, a_n, b_1, b_n, c_0, c_1, c_n, T_0, S_n (sympy + string), initials
641
+ """
642
+ import sympy as sp
643
+ import re
644
+
645
+ # Naprawiamy brak separatora między warunkiem początkowym a równaniem
646
+ equation_text = re.sub(r"(T_0\s*=\s*[0-9\+\-\*/\^\(\)n]+)\s+(?=[0-9]+\*?T_n)", r"\1, ", equation_text)
647
+ equation_text = re.sub(r"(T_0\s*=\s*[0-9\+\-\*/\^\(\)n]+)\s+(?=[0-9]+\*?T_1)", r"\1, ", equation_text)
648
+
649
+ # Normalizacja wejścia
650
+ equation_text = equation_text.replace("^", "**")
651
+ equation_text = re.sub(r"\s+", " ", equation_text.strip())
652
+
653
+ n = N_SYMBOL # UŻYJ GLOBALNEGO SYMBOLU
654
+ T = sp.Function("T")
655
+
656
+ print(f"Symbol n created: {n}, id: {id(n)}")
657
+ print("=== RAW INPUT ===")
658
+ print(equation_text)
659
+
660
+ # Rozdziel równania po przecinku lub średniku
661
+ equations = re.split(r'[,;]\s*', equation_text)
662
+ equations = [eq.strip() for eq in equations if eq.strip()]
663
+
664
+ print(f"Znaleziono {len(equations)} równań:")
665
+ for i, eq in enumerate(equations):
666
+ print(f" [{i}]: {eq}")
667
+
668
+ # Inicjalizacja zmiennych
669
+ a_0 = sp.Integer(1)
670
+ a_1 = None
671
+ a_n = None
672
+ b_1 = None
673
+ b_n = None
674
+ c_0 = None
675
+ c_1 = None
676
+ c_n = None
677
+ T_0 = None
678
+ T_1 = None
679
+ inits = []
680
+
681
+ local_dict = {'n': n, 'T': T, 'factorial': sp.factorial}
682
+
683
+ # Przetwarzanie każdego równania
684
+ for eq in equations:
685
+ # Obsługa factorial w notacji !
686
+ eq_processed = re.sub(r'(\w+)!', r'factorial(\1)', eq)
687
+
688
+ # Sprawdź, czy to warunek początkowy T_0
689
+ if re.match(r'.*T_0\s*=', eq_processed):
690
+ match = re.match(r'([0-9\*\^\+\-\s/\(\)]*)T_0\s*=\s*(.+)', eq_processed)
691
+ if match:
692
+ coeff_str = match.group(1).strip()
693
+ val_str = match.group(2).strip()
694
+ a_0 = sp.sympify(coeff_str, locals=local_dict) if coeff_str else sp.Integer(1)
695
+ c_0 = sp.sympify(val_str, locals=local_dict)
696
+ T_0 = sp.simplify(c_0 / a_0)
697
+ inits.append((0, T_0))
698
+ print(f" → T_0: a_0={a_0}, c_0={c_0}, T_0={T_0}")
699
+ continue
700
+
701
+ # Sprawdź, czy to równanie dla T_1
702
+ if re.match(r'.*T_1\s*=', eq_processed):
703
+ # Może być w postaci: T_1 = T_0 + ... lub a_1*T_1 = b_1*T_0 + c_1
704
+ eq_processed = eq_processed.replace("T_0", "T(0)").replace("T_1", "T(1)")
705
+
706
+ if "=" in eq_processed:
707
+ lhs_str, rhs_str = eq_processed.split("=", 1)
708
+ lhs = sp.sympify(lhs_str.strip(), locals=local_dict)
709
+ rhs = sp.sympify(rhs_str.strip(), locals=local_dict)
710
+
711
+ # Wyciągnij współczynniki
712
+ a_1 = lhs.expand().coeff(T(1)) or sp.Integer(1)
713
+ b_1 = rhs.expand().coeff(T(0)) or sp.Integer(0)
714
+ c_1_raw = rhs - b_1 * T(0)
715
+ c_1 = sp.simplify(c_1_raw.replace(T, lambda x: 0))
716
+
717
+ # Oblicz T_1 jeśli mamy T_0
718
+ if T_0 is not None:
719
+ T_1 = sp.simplify((b_1 * T_0 + c_1) / a_1)
720
+ inits.append((1, T_1))
721
+
722
+ print(f" → T_1: a_1={a_1}, b_1={b_1}, c_1={c_1}, T_1={T_1}")
723
+ continue
724
+
725
+ # Równanie główne dla T_n
726
+ if "T_n" in eq or "T_{n-1}" in eq:
727
+ eq_processed = eq_processed.replace("T_{n-1}", "T(n-1)").replace("T_n", "T(n)")
728
+
729
+ if "=" in eq_processed:
730
+ lhs_str, rhs_str = eq_processed.split("=", 1)
731
+ lhs = sp.sympify(lhs_str.strip(), locals=local_dict)
732
+ rhs = sp.sympify(rhs_str.strip(), locals=local_dict)
733
+
734
+ a_n = lhs.expand().coeff(T(n)) or sp.Integer(1)
735
+ b_n = rhs.expand().coeff(T(n-1)) or sp.Integer(0)
736
+ c_n_raw = rhs - b_n * T(n-1)
737
+ c_n = sp.simplify(c_n_raw.replace(T, lambda x: 0))
738
+
739
+ print(f" → T_n: a_n={a_n}, b_n={b_n}, c_n={c_n}")
740
+
741
+ # Jeśli nie podano a_1, oblicz z a_n dla n=1
742
+ if a_1 is None and a_n is not None:
743
+ a_1 = sp.simplify(a_n.subs(n, 1))
744
+ print(f" → a_1 obliczone z a_n: {a_1}")
745
+
746
+ # Jeśli nie podano b_1, oblicz z b_n dla n=1
747
+ if b_1 is None and b_n is not None:
748
+ b_1 = sp.simplify(b_n.subs(n, 1))
749
+ print(f" → b_1 obliczone z b_n: {b_1}")
750
+
751
+ # Jeśli nie podano c_1, oblicz z c_n dla n=1
752
+ if c_1 is None and c_n is not None:
753
+ c_1 = sp.simplify(c_n.subs(n, 1))
754
+ print(f" → c_1 obliczone z c_n: {c_1}")
755
+
756
+ # Zamień wszystkie symbole 'n' na N_SYMBOL
757
+ if a_n: a_n = a_n.subs(sp.Symbol('n'), N_SYMBOL)
758
+ if b_n: b_n = b_n.subs(sp.Symbol('n'), N_SYMBOL)
759
+ if c_n: c_n = c_n.subs(sp.Symbol('n'), N_SYMBOL)
760
+
761
+ print("\n1. a_0:", a_0, " a_n:", a_n, "b_n:", b_n," c_n:", c_n," c_0:", c_0," T_0:", T_0)
762
+
763
+ # Obliczenie S_n
764
+ print("\n=== Obliczanie S_n ===")
765
+ S_n_expr, S_n_str, S_1_expr, S_1_str = compute_Sn_Claude(a_0, a_n, b_n)
766
+
767
+ # Obliczenie 1/(S_n * a_n)
768
+ try:
769
+ inverse_Sn_an = sp.simplify(1 / (S_n_expr * a_n))
770
+ inverse_Sn_an = convert_negative_powers_to_fractions(inverse_Sn_an)
771
+ inverse_Sn_an = aggressive_simplify(inverse_Sn_an)
772
+ inverse_Sn_an = gamma_to_factorial(inverse_Sn_an)
773
+ inverse_Sn_an_str = to_latex_display_chat(inverse_Sn_an)
774
+ except Exception:
775
+ inverse_Sn_an_str = "?"
776
+
777
+ print("DEBUG 1/(S_n * a_n):", inverse_Sn_an_str)
778
+
779
+ # Obliczenie S_k * c_k
780
+ Sk_ck_expr, Sk_ck_str = generate_Sk_ck_product_new(S_n_expr, c_n, verbose=True)
781
+ print("DEBUG S_k * c_k:", Sk_ck_str)
782
+
783
+ # Obliczenie sumy
784
+ sum_Sk_ck_expr, sum_Sk_ck_str = compute_sum_Sk_ck(Sk_ck_expr, n, verbose=True)
785
+ print(f"DEBUG suma S_k*c_k: {sum_Sk_ck_str}")
786
+
787
+ # Obliczenie końcowego T_n
788
+ T_n_expr, T_n_str = compute_final_Tn(S_n_expr, S_1_expr, a_n, b_1, T_0, sum_Sk_ck_expr, verbose=True)
789
+ print(f"DEBUG T_n (final): {T_n_str}")
790
+
791
+ return {
792
+ "a_0": to_latex_display_chat(a_0),
793
+ "a_1": to_latex_display_chat(a_1) if a_1 else "?",
794
+ "a_n": to_latex_display_chat(a_n),
795
+ "b_1": to_latex_display_chat(b_1),
796
+ "b_n": to_latex_display_chat(b_n),
797
+ "c_0": to_latex_display_chat(c_0) if c_0 else "?",
798
+ "c_1": to_latex_display_chat(c_1) if c_1 else "?",
799
+ "c_n": to_latex_display_chat(c_n),
800
+ "T_0": T_0,
801
+ "S_n": to_latex_display_chat(S_n_str),
802
+ "S_n_sym": S_n_expr,
803
+ "S_1": S_1_str,
804
+ "S_1_sym": S_1_expr,
805
+ "inverse_Sn_an": to_latex_display_chat(inverse_Sn_an_str),
806
+ "Sk_ck": Sk_ck_str,
807
+ "SUMA": sum_Sk_ck_str,
808
+ "T_n": T_n_str,
809
+ "T_n_sym": T_n_expr,
810
+ "initials": [(0, T_0), (1, T_1)] if T_1 else [(0, T_0)]
811
+ }
812
 
813
 
814
  # --- Dopasowanie podobnych zadań ---
 
822
  # --- Tryb wskazówek ---
823
  def hint_mode(user_problem, step_idx=0):
824
  try:
825
+ coeffs = parse_recurrence_equation_3(user_problem)
826
  for k, v in coeffs.items():
827
  if hasattr(v, "__str__"):
828
  coeffs[k] = str(v)