Opened 8 years ago

Closed 2 years ago

#11541 closed enhancement (fixed)

Add example with solution_dict and substituting in a matrix

Reported by: kcrisman Owned by: mvngu
Priority: minor Milestone: sage-6.9
Component: documentation Keywords: beginner matrix solve symbolic sd90
Cc: Merged in:
Authors: Travis Scholl Reviewers: Vincent Delecroix, Aly Deines
Report Upstream: N/A Work issues:
Branch: a925d7e (Commits) Commit: a925d7e71ff32acc4d3969adf9307318409d335f
Dependencies: Stopgaps:

Description

See this ask.sagemath.org question. The poster wants an example something like

f(x,y)=x^2*y+y^2+y
solutions=solve(list(f.diff()),[x,y])
the_solution=solutions[2]
H=f.diff(2);  # Hessian matrix

but showing this.

sage: solutions=solve(list(f.diff()),[x,y],solution_dict=True)
sage: solutions
[{y: 0, x: -I}, {y: 0, x: I}, {y: -1/2, x: 0}]
sage: H.subs(solutions[2])
[(x, y) |--> -1  (x, y) |--> 0]
[ (x, y) |--> 0  (x, y) |--> 2]
sage: H(x,y).subs(solutions[2])
[-1  0]
[ 0  2]

This seems like a reasonable request.

Change History (21)

comment:1 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:2 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:3 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:4 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:5 Changed 4 years ago by tscholl2

  • Authors set to Travis Scholl
  • Branch set to u/tscholl2/matrix_solution_11541
  • Commit set to 3843197f73f407aa9c5f733de056a8b18742bcc1
  • Status changed from new to needs_review

Added two examples in symbolic expressions on:

http://doc.sagemath.org/html/en/reference/calculus/sage/symbolic/relation.html

One is substituting variables into matrices of symbolic functions and the other is solving a multivariate equation and substituting the solution into a matrix.


New commits:

3843197Added example subtituting values into a matrix of symbolic functions.

comment:6 Changed 4 years ago by vdelecroix

  • Reviewers set to Vincent Delecroix
  • Status changed from needs_review to needs_work

Hello,

Could you use A = B instead of A=B? You did it in the first example but not the second. Something like

sage: solutions = solve(list(f.diff()), [x,y], solution_dict=True)

is much easier to read.

Vincent

comment:7 Changed 4 years ago by git

  • Commit changed from 3843197f73f407aa9c5f733de056a8b18742bcc1 to b26b32768c3338c31b515bc5c00d443ff9c05948

Branch pushed to git repo; I updated commit sha1. New commits:

b26b327fixed spacing

comment:8 Changed 4 years ago by tscholl2

  • Status changed from needs_work to needs_review

Thanks for pointing that out!

I added some spacing. Is there anything else you noticed?

comment:9 Changed 4 years ago by vdelecroix

  • Status changed from needs_review to positive_review

That was all!

comment:10 Changed 4 years ago by chapoton

  • Milestone changed from sage-6.4 to sage-6.9

comment:11 Changed 4 years ago by vbraun

  • Status changed from positive_review to needs_work

Doctest depends on dictionary order of symbolic variables:

sage -t --long src/sage/symbolic/relation.py
**********************************************************************
File "src/sage/symbolic/relation.py", line 158, in sage.symbolic.relation
Failed example:
    solutions
Expected:
    [{y: 0, x: -I}, {y: 0, x: I}, {y: -1/2, x: 0}]
Got:
    [{x: -I, y: 0}, {x: I, y: 0}, {x: 0, y: -1/2}]
**********************************************************************
1 item had failures:
   1 of 131 in sage.symbolic.relation
    [295 tests, 1 failure, 7.50 s]

comment:12 Changed 4 years ago by git

  • Commit changed from b26b32768c3338c31b515bc5c00d443ff9c05948 to 97ee2c17f8010735e26b4b29dc95e2063a7f5e83

Branch pushed to git repo; I updated commit sha1. New commits:

97ee2c1Merge tag '6.9.beta6' into matrix_solution_11541

comment:13 Changed 4 years ago by tscholl2

I didn't get that error when I tested the file. I merged in the latest release and I still don't get the error. Do you think it could be my Sage installation? It's a pretty clean version except for an optional package for another branch I was working on. Are other people getting this error?

comment:14 Changed 4 years ago by vbraun

The doctest sorts dictionaries, but the sort order of symbolic variables is random (and depends strongly on memory locations / the OS). Just rewrite the test as something that doesn't depend on random orders, e.g.

solutions == [{x: -I, y: 0}, {x: I, y: 0}, {x: 0, y: -1/2}]

comment:15 Changed 4 years ago by git

  • Commit changed from 97ee2c17f8010735e26b4b29dc95e2063a7f5e83 to 5307163434b35889cc6b166d386095ccd482afa6

Branch pushed to git repo; I updated commit sha1. New commits:

5307163removed dependency on order of dictionary in doctest

comment:16 Changed 4 years ago by tscholl2

Interesting, I had no idea the order of symbolic variables was that random.

I updated the doc test using the equality check instead and it seems to work great. Thanks for the fix!

I noticed there was some other tests not using equality checks like this on lines 136 and 143 (and probably more), which is probably where I copied my test from. Should this be a separate ticket or should I modify it on this branch as well?

comment:17 Changed 2 years ago by git

  • Commit changed from 5307163434b35889cc6b166d386095ccd482afa6 to a925d7e71ff32acc4d3969adf9307318409d335f

Branch pushed to git repo; I updated commit sha1. New commits:

a925d7eMerge branch 'develop' into 11541

comment:18 Changed 2 years ago by tscholl2

I found that the other tests did not rely on multiple variables, so they are fine.

I also brought this branch up to date and checked that the docs still build.

comment:19 Changed 2 years ago by tscholl2

  • Status changed from needs_work to needs_review

comment:20 Changed 2 years ago by aly.deines

  • Keywords sd90 added
  • Reviewers changed from Vincent Delecroix to Vincent Delecroix, Aly Deines
  • Status changed from needs_review to positive_review

The doctests pass and the documentation builds and looks good.

comment:21 Changed 2 years ago by vbraun

  • Branch changed from u/tscholl2/matrix_solution_11541 to a925d7e71ff32acc4d3969adf9307318409d335f
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.