{
  "__type": "IngestedDoc",
  "__tag": 4010,
  "_content": {
    "Notes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The iterative loop runs "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "maxit=maxiter"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " (20 if "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "maxit=None"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ") iterations at most and finishes earlier if the tolerance is met. Breaking backward compatibility with the previous version, LOBPCG now returns the block of iterative vectors with the best accuracy rather than the last one iterated, as a cure for possible divergence."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "X.dtype == np.float32"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and user-provided operations/multiplications by "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "A"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "B"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", and "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "M"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " all preserve the "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "np.float32"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " data type, all the calculations and the output are in "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "np.float32"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The size of the iteration history output equals to the number of the best (limited by "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "maxit",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ") iterations plus 3: initial, final, and postprocessing."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "If both "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "retLambdaHistory"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "ParamRef",
              "__tag": 4071,
              "name": "retResidualNormsHistory"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " are "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "True"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", the return tuple has the following format "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "(lambda, V, lambda history, residual norms history)"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "In the following "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " denotes the matrix size and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "k"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " the number of required eigenvalues (smallest or largest)."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The LOBPCG code internally solves eigenproblems of the size "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "3k"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " on every iteration by calling the dense eigensolver "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "eigh",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", so if "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "k"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is not small enough compared to "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", it makes no sense to call the LOBPCG code. Moreover, if one calls the LOBPCG algorithm for "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "5k > n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", it would likely break internally, so the code calls the standard function "
            },
            {
              "__type": "InlineRole",
              "__tag": 4003,
              "value": "eigh",
              "domain": null,
              "role": null,
              "inventory": null
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " instead. It is not that "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " should be large for the LOBPCG to work, but rather the ratio "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n / k"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " should be large. It you call LOBPCG with "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "k=1"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " and "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n=10"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": ", it works though "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": " is small. The method is intended for extremely large "
            },
            {
              "__type": "InlineCode",
              "__tag": 4051,
              "value": "n / k"
            },
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "."
            }
          ]
        },
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "The convergence speed depends basically on three factors:"
            }
          ]
        },
        {
          "__type": "BulletList",
          "__tag": 4053,
          "ordered": true,
          "start": 1,
          "children": [
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Quality of the initial approximations "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "X"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " to the seeking eigenvectors.    Randomly distributed around the origin vectors work well if no better    choice is known."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Relative separation of the desired eigenvalues from the rest    of the eigenvalues. One can vary "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "k"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " to improve the separation."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "ListItem",
              "__tag": 4054,
              "children": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Proper preconditioning to shrink the spectral spread.    For example, a rod vibration test problem (under tests    directory) is ill-conditioned for large "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "n"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", so convergence will be    slow, unless efficient preconditioning is used. For this specific    problem, a good simple preconditioner function would be a linear solve    for "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "A"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", which is easy to code since "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "A"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is tridiagonal."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Raises": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Yields": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Methods": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Returns": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "lambda",
              "annotation": "ndarray of the shape ``(k, )``.",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Array of "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "k"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " approximate eigenvalues."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "v",
              "annotation": "ndarray of the same shape as ``X.shape``.",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "An array of "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "k"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " approximate eigenvectors."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "lambdaHistory",
              "annotation": "ndarray, optional.",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The eigenvalue history, if "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "retLambdaHistory"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "ResidualNormsHistory",
              "annotation": "ndarray, optional.",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The history of residual norms, if "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "retResidualNormsHistory"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "True"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "Locally Optimal Block Preconditioned Conjugate Gradient Method (LOBPCG)."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Receives": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Warnings": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Attributes": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    },
    "Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Parameters",
          "__tag": 4026,
          "children": [
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "A",
              "annotation": "{sparse matrix, ndarray, LinearOperator, callable object}",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The Hermitian linear operator of the problem, usually given by a sparse matrix.  Often called the \"stiffness matrix\"."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "X",
              "annotation": "ndarray, float32 or float64",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Initial approximation to the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "k"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " eigenvectors (non-sparse). If "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "A"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " has "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "shape=(n,n)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " then "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "X"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " must have "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "shape=(n,k)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "B",
              "annotation": "{sparse matrix, ndarray, LinearOperator, callable object}",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Optional. By default "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "B = None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", which is equivalent to identity. The right hand side operator in a generalized eigenproblem if present. Often called the \"mass matrix\". Must be Hermitian positive definite."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "M",
              "annotation": "{sparse matrix, ndarray, LinearOperator, callable object}",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Optional. By default "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "M = None"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", which is equivalent to identity. Preconditioner aiming to accelerate convergence."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "Y",
              "annotation": "ndarray, float32 or float64, default: None",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "An "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "n-by-sizeY"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " ndarray of constraints with "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "sizeY < n"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". The iterations will be performed in the "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "B"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "-orthogonal complement of the column-space of "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "Y"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". "
                    },
                    {
                      "__type": "ParamRef",
                      "__tag": 4071,
                      "name": "Y"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " must be full rank if present."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "tol",
              "annotation": "scalar, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "The default is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "tol=n*sqrt(eps)"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". Solver tolerance for the stopping criterion."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "maxiter",
              "annotation": "int, default: 20",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Maximum number of iterations."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "largest",
              "annotation": "bool, default: True",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "When True, solve for the largest eigenvalues, otherwise the smallest."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "verbosityLevel",
              "annotation": "int, optional",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "By default "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "verbosityLevel=0"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " no output. Controls the solver standard/screen output."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "retLambdaHistory",
              "annotation": "bool, default: False",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Whether to return iterative eigenvalue history."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "retResidualNormsHistory",
              "annotation": "bool, default: False",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Whether to return iterative history of residual norms."
                    }
                  ]
                }
              ]
            },
            {
              "__type": "DocParam",
              "__tag": 4016,
              "name": "restartControl",
              "annotation": "int, optional.",
              "desc": [
                {
                  "__type": "Paragraph",
                  "__tag": 4045,
                  "children": [
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": "Iterations restart if the residuals jump "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "2**restartControl"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": " times compared to the smallest recorded in "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "retResidualNormsHistory"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ". The default is "
                    },
                    {
                      "__type": "InlineCode",
                      "__tag": 4051,
                      "value": "restartControl=20"
                    },
                    {
                      "__type": "Text",
                      "__tag": 4046,
                      "value": ", making the restarts rare for backward compatibility."
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Extended Summary": {
      "__type": "Section",
      "__tag": 4015,
      "children": [
        {
          "__type": "Paragraph",
          "__tag": 4045,
          "children": [
            {
              "__type": "Text",
              "__tag": 4046,
              "value": "LOBPCG is a preconditioned eigensolver for large real symmetric and complex Hermitian definite generalized eigenproblems."
            }
          ]
        }
      ],
      "title": [],
      "level": 0,
      "target": null
    },
    "Other Parameters": {
      "__type": "Section",
      "__tag": 4015,
      "children": [],
      "title": [],
      "level": 0,
      "target": null
    }
  },
  "_ordered_sections": [
    "Summary",
    "Extended Summary",
    "Parameters",
    "Attributes",
    "Methods",
    "Returns",
    "Yields",
    "Receives",
    "Other Parameters",
    "Raises",
    "Warns",
    "Warnings",
    "Notes"
  ],
  "item_file": "/scipy/sparse/linalg/_eigen/lobpcg/lobpcg.py",
  "item_line": 169,
  "item_type": "function",
  "aliases": [
    "scipy.sparse.linalg.lobpcg"
  ],
  "example_section_data": {
    "__type": "Section",
    "__tag": 4015,
    "children": [
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "Our first example is minimalistic - find the largest eigenvalue of\na diagonal matrix by solving the non-generalized eigenvalue problem\n``A x = lambda x`` without constraints or preconditioning.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "import numpy as np\nfrom scipy.sparse import diags_array\nfrom scipy.sparse.linalg import LinearOperator, aslinearoperator\nfrom scipy.sparse.linalg import lobpcg\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe square matrix size is\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "n = 100\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nand its diagonal entries are 1, ..., 100 defined by\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "vals = np.arange(1, n + 1).astype(np.int16)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe first mandatory input parameter in this test is\nthe sparse diagonal matrix `A`\nof the eigenvalue problem ``A x = lambda x`` to solve.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "A = diags_array(vals, offsets=0, shape=(n, n), dtype=None)\nA.toarray()\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe second mandatory input parameter `X` is a 2D array with the\nrow dimension determining the number of requested eigenvalues.\n`X` is an initial guess for targeted eigenvectors.\n`X` must have linearly independent columns.\nIf no initial approximations available, randomly oriented vectors\ncommonly work best, e.g., with components normally distributed\naround zero or uniformly distributed on the interval [-1 1].\nSetting the initial approximations to dtype ``np.float32``\nforces all iterative values to dtype ``np.float32`` speeding up\nthe run while still allowing accurate eigenvalue computations.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "k = 1\nrng = np.random.default_rng()\nX = rng.normal(size=(n, k))\nX = X.astype(np.float32)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A, X, maxiter=60)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\n`lobpcg` needs only access the matrix product with `A` rather\nthen the matrix itself. Since the matrix `A` is diagonal in\nthis example, one can write a function of the matrix product\n``A @ X`` using the diagonal values ``vals`` only, e.g., by\nelement-wise multiplication with broadcasting in the lambda-function\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "A_lambda = lambda X: vals[:, np.newaxis] * X\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nor the regular function\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "def A_matmat(X):\n    return vals[:, np.newaxis] * X\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nand use the handle to one of these callables as an input\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A_lambda, X, maxiter=60)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A_matmat, X, maxiter=60)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe traditional callable `LinearOperator` is no longer\nnecessary but still supported as the input to `lobpcg`.\nSpecifying ``matmat=A_matmat`` explicitly improves performance. \n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "A_lo = LinearOperator((n, n), matvec=A_matmat, matmat=A_matmat, dtype=np.int16)\neigenvalues, _ = lobpcg(A_lo, X, maxiter=80)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe least efficient callable option is `aslinearoperator`:\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(aslinearoperator(A), X, maxiter=80)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nWe now switch to computing the three smallest eigenvalues specifying\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "k = 3\nX = np.random.default_rng().normal(size=(n, k))\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nand ``largest=False`` parameter\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A, X, largest=False, maxiter=90)\nprint(eigenvalues)  \n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe next example illustrates computing 3 smallest eigenvalues of\nthe same matrix `A` given by the function handle ``A_matmat`` but\nwith constraints and preconditioning.\n\nConstraints - an optional input parameter is a 2D array comprising\nof column vectors that the eigenvectors must be orthogonal to\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "Y = np.eye(n, 3)\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nThe preconditioner acts as the inverse of `A` in this example, but\nin the reduced precision ``np.float32`` even though the initial `X`\nand thus all iterates and the output are in full ``np.float64``.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "inv_vals = 1./vals\ninv_vals = inv_vals.astype(np.float32)\nM = lambda X: inv_vals[:, np.newaxis] * X\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nLet us now solve the eigenvalue problem for the matrix `A` first\nwithout preconditioning requesting 80 iterations\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A_matmat, X, Y=Y, largest=False, maxiter=80)\neigenvalues\neigenvalues.dtype\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nWith preconditioning we need only 20 iterations from the same `X`\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A_matmat, X, Y=Y, M=M, largest=False, maxiter=20)\neigenvalues\n",
        "execution_status": "success"
      },
      {
        "__type": "Text",
        "__tag": 4046,
        "value": "\nNote that the vectors passed in `Y` are the eigenvectors of the 3\nsmallest eigenvalues. The results returned above are orthogonal to those.\n\nThe primary matrix `A` may be indefinite, e.g., after shifting\n``vals`` by 50 from 1, ..., 100 to -49, ..., 50, we still can compute\nthe 3 smallest or largest eigenvalues.\n\n"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "vals = vals - 50\nX = rng.normal(size=(n, k))\neigenvalues, _ = lobpcg(A_matmat, X, largest=False, maxiter=99)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues, _ = lobpcg(A_matmat, X, largest=True, maxiter=99)\n",
        "execution_status": "success"
      },
      {
        "__type": "Code",
        "__tag": 4050,
        "value": "eigenvalues\n",
        "execution_status": "failure"
      }
    ],
    "title": [],
    "level": 0,
    "target": null
  },
  "see_also": [],
  "signature": {
    "__type": "SignatureNode",
    "__tag": 4029,
    "kind": "function",
    "parameters": [
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "A",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "X",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": {
          "__type": "Empty",
          "__tag": 4031
        }
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "B",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "M",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "Y",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "tol",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "maxiter",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "None"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "largest",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "True"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "verbosityLevel",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "0"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "retLambdaHistory",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "False"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "retResidualNormsHistory",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "False"
      },
      {
        "__type": "SigParam",
        "__tag": 4030,
        "name": "restartControl",
        "annotation": {
          "__type": "Empty",
          "__tag": 4031
        },
        "kind": "POSITIONAL_OR_KEYWORD",
        "default": "20"
      }
    ],
    "return_annotation": {
      "__type": "Empty",
      "__tag": 4031
    },
    "target_name": "lobpcg"
  },
  "references": [
    ".. [1] A. V. Knyazev (2001),",
    "       Toward the Optimal Preconditioned Eigensolver: Locally Optimal",
    "       Block Preconditioned Conjugate Gradient Method.",
    "       SIAM Journal on Scientific Computing 23, no. 2,",
    "       pp. 517-541. :doi:`10.1137/S1064827500366124`",
    "",
    ".. [2] A. V. Knyazev, I. Lashuk, M. E. Argentati, and E. Ovchinnikov",
    "       (2007), Block Locally Optimal Preconditioned Eigenvalue Xolvers",
    "       (BLOPEX) in hypre and PETSc. :arxiv:`0705.2626`",
    "",
    ".. [3] A. V. Knyazev's C and MATLAB implementations:",
    "       https://github.com/lobpcg/blopex"
  ],
  "qa": "scipy.sparse.linalg._eigen.lobpcg.lobpcg:lobpcg",
  "arbitrary": [],
  "local_refs": [
    "A",
    "B",
    "M",
    "ResidualNormsHistory",
    "X",
    "Y",
    "lambda",
    "lambdaHistory",
    "largest",
    "maxiter",
    "restartControl",
    "retLambdaHistory",
    "retResidualNormsHistory",
    "tol",
    "v",
    "verbosityLevel"
  ]
}