Ticket #11618 (closed defect: fixed)

Opened 22 months ago

Last modified 21 months ago

solve with solution_dict=True doesn't always return a list

Reported by: mhampton Owned by: burcin
Priority: major Milestone: sage-4.7.2
Component: symbolics Keywords: solve, solution_dict
Cc: kcrisman Work issues:
Report Upstream: N/A Reviewers: Karl-Dieter Crisman
Authors: Leif Leonhardy Merged in: sage-4.7.2.alpha2
Dependencies: #8553 Stopgaps:

Description (last modified by kcrisman) (diff)

As pointed out in a post on sage-support:

var('x')
g(x)=0
print solve(g(x)==0,x)
print solve(g(x)==0,x,solution_dict=True) 

gives

[x == r1]
{x: r1}

It should be [{x: r1}] to be consistent.

Attachments

trac_11618-fix_solution_dict_for_infinitely_many_solutions.sagelib.patch Download (1.5 KB) - added by leif 22 months ago.
Sage library patch. (Updated version.) Based on Sage 4.7.1.rc0.

Change History

comment:1 Changed 22 months ago by leif

Yep:

       * ``solution_dict`` - bool (default: False); if True or non-zero,
         return a list of dictionaries containing the solutions. If there
         are no solutions, return an empty list (rather than a list
         containing an empty dictionary). Likewise, if there's only a
         single solution, return a list containing one dictionary with
         that solution.

A doctest for a similar case got deleted by a reviewer, which still works though:

sage: solve([x+y==6, x-y==4], x, y, solution_dict=-1)
[{y: 1, x: 5}]

And also the following works as expected:

sage: solve([x==0],x,solution_dict=True)
[{x: 0}]

(Sage 4.7.1.{alpha2,rc0})

comment:2 Changed 22 months ago by leif

  • Status changed from new to needs_review
  • Authors set to Leif Leonhardy

The attached patch fixes the inconsistency and adds a doctest for the case given.

I've removed superfluous parentheses (and white space) and also converted some tabs to spaces. (I thought the merge script would reject patches introducing new tabs after all got converted a while ago.)

comment:3 follow-up: ↓ 4 Changed 22 months ago by leif

Hmmm,

ans = [ {x: self.parent().var('r1')} ]

would perhaps have been a bit more readable...

Changed 22 months ago by leif

Sage library patch. (Updated version.) Based on Sage 4.7.1.rc0.

comment:4 in reply to: ↑ 3 Changed 22 months ago by leif

Replying to leif:

[...] would perhaps have been a bit more readable.

I've updated the patch to directly construct the solution dictionary.

(Leif doesn't work without coffee.)


Hey patchbot, apply: trac_11618-fix_solution_dict_for_infinitely_many_solutions.sagelib.patch Download!

comment:5 Changed 22 months ago by leif

  • Dependencies set to #8553

The dependency I've added is (more or less) just for the patchbot. (#8553 was merged into Sage 4.7.1.alpha2.)

comment:6 Changed 22 months ago by kcrisman

  • Cc kcrisman added

Thanks for posting this patch - I'll try to take a look at this (relatively) soon, as it looks straightforward.

comment:7 Changed 22 months ago by kcrisman

  • Status changed from needs_review to positive_review
  • Reviewers set to Karl-Dieter Crisman
  • Description modified (diff)

Patch looks good. Performs as advertised. Thanks!

comment:8 Changed 21 months ago by jdemeyer

  • Status changed from positive_review to closed
  • Resolution set to fixed
  • Merged in set to sage-4.7.2.alpha2
Note: See TracTickets for help on using tickets.