How can I preserve the value of my parameters through a function so it can be used multiple times with its initial value?

Vel Crow

I have just started using SAGE which is pretty close to python as I understand it, and I have come accross this problem where I'll have as a parameter of a function a matrix which I wish to use multiple times in the function with its same original function but through the different parts of the function it changes values.

I have seen in a tutorial that declaring a variable in the function as variable = list(parameter) doesnt affect the parameter or whatever is put in the parentheses. However I can't make it work.. Below is part of my program posing the problem (I can add the rest if necessary): I declare the variable determinant which has as value the result of the function my_Gauss_determinant with the variable auxmmatrix as parameter. Through the function my_Gauss_determinant the value of auxmmatrix changes but for some reason the value of mmatrix as well. How can avoid this and be able to re-use the parameter mmatrix with its original value?

def my_Cramer_solve(mmatrix,bb):
    auxmmatrix=list(mmatrix)
    determinant=my_Gauss_determinant(auxmmatrix)
    if determinant==0:
        print 
    k=len(auxmmatrix)
       solution=[]
        for l in range(k):
            auxmmatrix1=my_replace_column(list(mmatrix),l,bb)
            determinant1=my_Gauss_determinant(auxmmatrix1)
            solution.append(determinant1/determinant0)
    return  solution
Brendan Abel

What you want is a copy of mmatrix. The reason list(other_list) works is because it iterates through every item in other_list to create a new list. But the mutable objects within the list aren't copied

>>> a = [{1,2}]
>>> b = list(a)
>>> b[0].add(7)
>>> a
[set([1,2,7])]

To make a complete copy, you can use copy.deepcopy to make copies of the elements within the list

>>> import copy
>>> a = [{1,2}]
>>> b = copy.deepcopy(a)
>>> b[0].add(7)
>>> a
[set([1,2])]

So if you only want to copy the list, but don't want to copy the elements within the list, you would do this

auxmmatrix = copy.copy(matrix)
determinant = my_Gauss_determinant(copy.copy(matrix))

If you want to copy the elements within the list as well, use copy.deepcopy

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can i get the value of a variable through the Super keyword?

来自分类Dev

How can I return a value to a superclass without overriding the parent function?

来自分类Dev

How can I loop through values in my json file?

来自分类Dev

How can I loop through my array in reverse order?

来自分类Dev

How can i replace an objectAtIndex with value for key?

来自分类Dev

How can I find the string value of a property given it's value

来自分类Dev

How can I delete a user in linux when the system says its currently used in a process

来自分类Dev

Can parameters from a parametrized class be used in external function definitions?

来自分类Dev

how can i read cookie value that written by jquery?

来自分类Dev

How can I write key-value pairs in Prolog?

来自分类Dev

How can I change the value of a morsel of a cookie in a python CookieJar?

来自分类Dev

How can I used ReadAllLines with gzipped file

来自分类Dev

How can I return a function?

来自分类Dev

If i know how a traced output looks, how can i begin creating my function? (Scheme)

来自分类Dev

How can my data object reject changes from a databound control? (Control is not displaying actual value)

来自分类Dev

How can I "group" multiple rows into a collection in my PL/SQL cursor?

来自分类Dev

How can I make JSSOR change its aspect ratio?

来自分类Dev

How can I check to see if any values in an array show a value of true with an if statement?

来自分类Dev

How can I find out if an data- attribute is set to an empty value?

来自分类Dev

How can I increment value of the variable when scroll reaches top of the div

来自分类Dev

How can I set plain value (no json) for cookie in Yii2

来自分类Dev

How can I add values through XML in OpenERP7?

来自分类Dev

How can I recursively grep through several directories at once?

来自分类Dev

How can I pass floating point numbers as template parameters?

来自分类Dev

I can only make a cookie have one value in javascript

来自分类Dev

Why can't I call toString() on a double value?

来自分类Dev

Passing a value through Button to Php function

来自分类Dev

PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

来自分类Dev

How can I call super() so it's compatible in 2 and 3?

Related 相关文章

  1. 1

    How can i get the value of a variable through the Super keyword?

  2. 2

    How can I return a value to a superclass without overriding the parent function?

  3. 3

    How can I loop through values in my json file?

  4. 4

    How can I loop through my array in reverse order?

  5. 5

    How can i replace an objectAtIndex with value for key?

  6. 6

    How can I find the string value of a property given it's value

  7. 7

    How can I delete a user in linux when the system says its currently used in a process

  8. 8

    Can parameters from a parametrized class be used in external function definitions?

  9. 9

    how can i read cookie value that written by jquery?

  10. 10

    How can I write key-value pairs in Prolog?

  11. 11

    How can I change the value of a morsel of a cookie in a python CookieJar?

  12. 12

    How can I used ReadAllLines with gzipped file

  13. 13

    How can I return a function?

  14. 14

    If i know how a traced output looks, how can i begin creating my function? (Scheme)

  15. 15

    How can my data object reject changes from a databound control? (Control is not displaying actual value)

  16. 16

    How can I "group" multiple rows into a collection in my PL/SQL cursor?

  17. 17

    How can I make JSSOR change its aspect ratio?

  18. 18

    How can I check to see if any values in an array show a value of true with an if statement?

  19. 19

    How can I find out if an data- attribute is set to an empty value?

  20. 20

    How can I increment value of the variable when scroll reaches top of the div

  21. 21

    How can I set plain value (no json) for cookie in Yii2

  22. 22

    How can I add values through XML in OpenERP7?

  23. 23

    How can I recursively grep through several directories at once?

  24. 24

    How can I pass floating point numbers as template parameters?

  25. 25

    I can only make a cookie have one value in javascript

  26. 26

    Why can't I call toString() on a double value?

  27. 27

    Passing a value through Button to Php function

  28. 28

    PhantomJS; If I set a variable inside page.evaluate(), how can I access the value of that variable outside of page.evaluate()

  29. 29

    How can I call super() so it's compatible in 2 and 3?

热门标签

归档