@@ -214,3 +214,60 @@ describe('expr.solve()', () => {
214214 ` ) ;
215215 } ) ;
216216} ) ;
217+
218+ // Regression tests for #242: solve() should work for equations with variables
219+ // in the numerator of fractions (e.g., F = 3g/h solving for g)
220+ describe ( 'SOLVING EQUATIONS WITH FRACTIONS (#242)' , ( ) => {
221+ test ( 'should solve F = 3g/h for g' , ( ) => {
222+ const e = expr ( 'F = 3g/h' ) ;
223+ const result = e . solve ( 'g' ) ?. map ( ( x ) => x . toString ( ) ) ;
224+ expect ( result ) . toMatchInlineSnapshot ( `
225+ [
226+ 1/3 * F * h,
227+ ]
228+ ` ) ;
229+ } ) ;
230+
231+ test ( 'should solve x/2 + 3 = 0 for x' , ( ) => {
232+ const e = expr ( 'x/2 + 3 = 0' ) ;
233+ const result = e . solve ( 'x' ) ?. map ( ( x ) => x . json ) ;
234+ expect ( result ) . toEqual ( [ - 6 ] ) ;
235+ } ) ;
236+
237+ test ( 'should solve a/b + c*g/d = 0 for g' , ( ) => {
238+ const e = expr ( 'a/b + c*g/d = 0' ) ;
239+ const result = e . solve ( 'g' ) ?. map ( ( x ) => x . toString ( ) ) ;
240+ expect ( result ) . toMatchInlineSnapshot ( `
241+ [
242+ -(a * d) / (b * c),
243+ ]
244+ ` ) ;
245+ } ) ;
246+
247+ test ( 'should solve equation with multiple fractional terms' , ( ) => {
248+ const e = expr ( 'x/2 + x/3 = 5' ) ;
249+ const result = e . solve ( 'x' ) ?. map ( ( x ) => x . json ) ;
250+ expect ( result ) . toEqual ( [ 6 ] ) ;
251+ } ) ;
252+
253+ // This test will be enabled after implementing variable-in-denominator solving
254+ test . skip ( 'should solve a/x = b for x (variable in denominator)' , ( ) => {
255+ const e = expr ( 'a/x = b' ) ;
256+ const result = e . solve ( 'x' ) ?. map ( ( x ) => x . toString ( ) ) ;
257+ expect ( result ) . toMatchInlineSnapshot ( `
258+ [
259+ a / b,
260+ ]
261+ ` ) ;
262+ } ) ;
263+
264+ test ( 'should solve 1/(x+1) = 2 for x' , ( ) => {
265+ const e = expr ( '1/(x+1) = 2' ) ;
266+ const result = e . solve ( 'x' ) ?. map ( ( x ) => x . toString ( ) ) ;
267+ expect ( result ) . toMatchInlineSnapshot ( `
268+ [
269+ -1/2,
270+ ]
271+ ` ) ;
272+ } ) ;
273+ } ) ;
0 commit comments