Opened 10 years ago

Closed 10 years ago

#11618 closed defect (fixed)

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 Merged in: sage-4.7.2.alpha2
Authors: Leif Leonhardy Reviewers: Karl-Dieter Crisman
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: #8553 Stopgaps:

Status badges

Description (last modified by kcrisman)

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 (1)

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

Download all attachments as: .zip

Change History (9)

comment:1 Changed 10 years 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 10 years ago by leif

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

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: Changed 10 years ago by leif

Hmmm,

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

would perhaps have been a bit more readable...

Changed 10 years ago by leif

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

comment:4 in reply to: ↑ 3 Changed 10 years 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!

comment:5 Changed 10 years 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 10 years 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 10 years ago by kcrisman

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

Patch looks good. Performs as advertised. Thanks!

comment:8 Changed 10 years ago by jdemeyer

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