





import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    MegatronBertForCausalLM,
	    MegatronBertForMaskedLM,
	    MegatronBertForMultipleChoice,
	    MegatronBertForNextSentencePrediction,
	    MegatronBertForPreTraining,
	    MegatronBertForQuestionAnswering,
	    MegatronBertForSequenceClassification,
	    MegatronBertForTokenClassification,
	    MegatronBertModel,
	)



class    __lowerCAmelCase			:






			def __init__(self   ,				__magic_name__   ,				__magic_name__=13   ,				__magic_name__=7   ,				__magic_name__=True   ,				__magic_name__=True   ,				__magic_name__=True   ,				__magic_name__=True   ,				__magic_name__=99   ,				__magic_name__=64   ,				__magic_name__=32   ,				__magic_name__=5   ,				__magic_name__=4   ,				__magic_name__=37   ,				__magic_name__="gelu"   ,				__magic_name__=0.1   ,				__magic_name__=0.1   ,				__magic_name__=512   ,				__magic_name__=16   ,				__magic_name__=2   ,				__magic_name__=0.02   ,				__magic_name__=3   ,				__magic_name__=4   ,				__magic_name__=None   ,				)	->  int:


							'''simple docstring'''



							snake_case_		:       Optional[Any]           =    parent
							snake_case_		:       Tuple           =    batch_size
							snake_case_		:       List[Any]           =    seq_length
							snake_case_		:       Union[str, Any]           =    is_training
							snake_case_		:       Optional[Any]           =    use_input_mask
							snake_case_		:       Dict           =    use_token_type_ids
							snake_case_		:       str           =    use_labels
							snake_case_		:       Dict           =    vocab_size
							snake_case_		:       Dict           =    hidden_size
							snake_case_		:       Any           =    embedding_size
							snake_case_		:       int           =    num_hidden_layers
							snake_case_		:       Optional[Any]           =    num_attention_heads
							snake_case_		:       Optional[Any]           =    intermediate_size
							snake_case_		:       Any           =    hidden_act
							snake_case_		:       List[str]           =    hidden_dropout_prob
							snake_case_		:       List[Any]           =    attention_probs_dropout_prob
							snake_case_		:       Optional[Any]           =    max_position_embeddings
							snake_case_		:       Optional[Any]           =    type_vocab_size
							snake_case_		:       Optional[Any]           =    type_sequence_label_size
							snake_case_		:       Union[str, Any]           =    initializer_range
							snake_case_		:       Dict           =    num_labels
							snake_case_		:       Union[str, Any]           =    num_choices
							snake_case_		:       Tuple           =    scope






			def      lowerCamelCase    (self       )	->  List[Any]:


							'''simple docstring'''



							snake_case_		:       Tuple           =    ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size       )

							snake_case_		:       Union[str, Any]           =    None
							if self.use_input_mask:
											snake_case_		:       Tuple           =    random_attention_mask([self.batch_size, self.seq_length]       )

							snake_case_		:       Any           =    None
							if self.use_token_type_ids:
											snake_case_		:       Optional[Any]           =    ids_tensor([self.batch_size, self.seq_length]   ,				self.type_vocab_size       )

							snake_case_		:       int           =    None
							snake_case_		:       List[str]           =    None
							snake_case_		:       Any           =    None
							if self.use_labels:
											snake_case_		:       Union[str, Any]           =    ids_tensor([self.batch_size]   ,				self.type_sequence_label_size       )
											snake_case_		:       Optional[int]           =    ids_tensor([self.batch_size, self.seq_length]   ,				self.num_labels       )
											snake_case_		:       Any           =    ids_tensor([self.batch_size]   ,				self.num_choices       )

							snake_case_		:       List[Any]           =    self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






			def      lowerCamelCase    (self       )	->  Any:


							'''simple docstring'''



							return MegatronBertConfig(
							    vocab_size=self.vocab_size   ,				hidden_size=self.hidden_size   ,				num_hidden_layers=self.num_hidden_layers   ,				num_attention_heads=self.num_attention_heads   ,				intermediate_size=self.intermediate_size   ,				embedding_size=self.embedding_size   ,				hidden_act=self.hidden_act   ,				hidden_dropout_prob=self.hidden_dropout_prob   ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,				max_position_embeddings=self.max_position_embeddings   ,				type_vocab_size=self.type_vocab_size   ,				is_decoder=__magic_name__   ,				initializer_range=self.initializer_range   ,				)






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  Optional[Any]:


							'''simple docstring'''



							snake_case_		:       Tuple           =    MegatronBertModel(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Dict           =    model(__magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__       )
							snake_case_		:       Union[str, Any]           =    model(__magic_name__   ,				token_type_ids=__magic_name__       )
							snake_case_		:       List[Any]           =    model(__magic_name__       )

							self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)       )
							self.parent.assertEqual(result.pooler_output.shape   ,				(self.batch_size, self.hidden_size)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  int:


							'''simple docstring'''



							snake_case_		:       Any           =    MegatronBertForMaskedLM(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       str           =    model(__magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__       )
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  List[Any]:


							'''simple docstring'''



							snake_case_		:       List[Any]           =    MegatronBertForCausalLM(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Union[str, Any]           =    model(__magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__       )
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  List[Any]:


							'''simple docstring'''



							snake_case_		:       Optional[Any]           =    MegatronBertForNextSentencePrediction(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Optional[int]           =    model(
							    __magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__   ,				)
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, 2)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  Any:


							'''simple docstring'''



							snake_case_		:       str           =    MegatronBertForPreTraining(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Tuple           =    model(
							    __magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__   ,				next_sentence_label=__magic_name__   ,				)
							self.parent.assertEqual(result.prediction_logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)       )
							self.parent.assertEqual(result.seq_relationship_logits.shape   ,				(self.batch_size, 2)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  List[Any]:


							'''simple docstring'''



							snake_case_		:       Optional[Any]           =    MegatronBertForQuestionAnswering(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Dict           =    model(
							    __magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				start_positions=__magic_name__   ,				end_positions=__magic_name__   ,				)
							self.parent.assertEqual(result.start_logits.shape   ,				(self.batch_size, self.seq_length)       )
							self.parent.assertEqual(result.end_logits.shape   ,				(self.batch_size, self.seq_length)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  Tuple:


							'''simple docstring'''



							snake_case_		:       List[Any]           =    self.num_labels
							snake_case_		:       Optional[Any]           =    MegatronBertForSequenceClassification(__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       List[Any]           =    model(__magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__       )
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  int:


							'''simple docstring'''



							snake_case_		:       List[Any]           =    self.num_labels
							snake_case_		:       List[str]           =    MegatronBertForTokenClassification(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       Optional[Any]           =    model(__magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__       )
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels)       )






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )	->  List[Any]:


							'''simple docstring'''



							snake_case_		:       Dict           =    self.num_choices
							snake_case_		:       List[Any]           =    MegatronBertForMultipleChoice(config=__magic_name__       )
							model.to(__magic_name__       )
							model.eval()
							snake_case_		:       int           =    input_ids.unsqueeze(1       ).expand(-1   ,				self.num_choices   ,				-1       ).contiguous()
							snake_case_		:       List[str]           =    token_type_ids.unsqueeze(1       ).expand(-1   ,				self.num_choices   ,				-1       ).contiguous()
							snake_case_		:       Optional[Any]           =    input_mask.unsqueeze(1       ).expand(-1   ,				self.num_choices   ,				-1       ).contiguous()
							snake_case_		:       List[str]           =    model(
							    __magic_name__   ,				attention_mask=__magic_name__   ,				token_type_ids=__magic_name__   ,				labels=__magic_name__   ,				)
							self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)       )






			def      lowerCamelCase    (self       )	->  int:


							'''simple docstring'''



							snake_case_		:       Optional[int]           =    self.prepare_config_and_inputs()
							(
							    (
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,				(
							    snake_case_			
							)     ,							
							)		:       List[Any]           =    config_and_inputs
							snake_case_		:       int           =    {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
							return config, inputs_dict




@require_torch
class    __lowerCAmelCase			(     _a,	_a,	unittest.TestCase	):
			lowerCamelCase_	:	List[str]			     = (
			    (
			        MegatronBertModel,
			        MegatronBertForMaskedLM,
			        MegatronBertForCausalLM,
			        MegatronBertForMultipleChoice,
			        MegatronBertForNextSentencePrediction,
			        MegatronBertForPreTraining,
			        MegatronBertForQuestionAnswering,
			        MegatronBertForSequenceClassification,
			        MegatronBertForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase_	:	Dict			     = (
			    {
			        '''feature-extraction''': MegatronBertModel,
			        '''fill-mask''': MegatronBertForMaskedLM,
			        '''question-answering''': MegatronBertForQuestionAnswering,
			        '''text-classification''': MegatronBertForSequenceClassification,
			        '''text-generation''': MegatronBertForCausalLM,
			        '''token-classification''': MegatronBertForTokenClassification,
			        '''zero-shot''': MegatronBertForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase_	:	Optional[int]			     = True
			# test_resize_embeddings = False
			lowerCamelCase_	:	Union[str, Any]			     = False






			def      lowerCamelCase    (self   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__=False       )	->  Optional[Any]:


							'''simple docstring'''



							snake_case_		:       str           =    super()._prepare_for_class(__magic_name__   ,				__magic_name__   ,				return_labels=__magic_name__       )

							if return_labels:
											if model_class in get_values(__magic_name__       ):
															snake_case_		:       Optional[Any]           =    torch.zeros(
															    (self.model_tester.batch_size, self.model_tester.seq_length)   ,				dtype=torch.long   ,				device=__magic_name__       )
															snake_case_		:       Dict           =    torch.zeros(
															    self.model_tester.batch_size   ,				dtype=torch.long   ,				device=__magic_name__       )
							return inputs_dict






			def      lowerCamelCase    (self       )	->  Union[str, Any]:


							'''simple docstring'''



							snake_case_		:       Tuple           =    MegatronBertModelTester(self       )
							snake_case_		:       List[Any]           =    ConfigTester(self   ,				config_class=__magic_name__   ,				hidden_size=37       )






			def      lowerCamelCase    (self       )	->  Any:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def      lowerCamelCase    (self       )	->  List[str]:


							'''simple docstring'''



							snake_case_		:       List[Any]           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_model(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Optional[int]:


							'''simple docstring'''



							snake_case_		:       List[str]           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_masked_lm(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Optional[int]:


							'''simple docstring'''



							snake_case_		:       Dict           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Optional[Any]:


							'''simple docstring'''



							snake_case_		:       Dict           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Optional[int]:


							'''simple docstring'''



							snake_case_		:       Tuple           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_pretraining(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Dict:


							'''simple docstring'''



							snake_case_		:       Tuple           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_question_answering(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  Optional[int]:


							'''simple docstring'''



							snake_case_		:       int           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*__magic_name__       )






			def      lowerCamelCase    (self       )	->  List[str]:


							'''simple docstring'''



							snake_case_		:       Optional[Any]           =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_megatron_bert_for_token_classification(*__magic_name__       )






def 	lowerCamelCase_		(							_UpperCamelCase					)						->		Dict:






				"""simple docstring"""


				return torch.tensor(
				    __snake_case					,							dtype=torch.long					,							device=__snake_case					,							)


lowerCAmelCase_        =							1e-4





@require_torch
@require_sentencepiece
@require_tokenizers
class    __lowerCAmelCase			(     unittest.TestCase	):






			@slow
			@unittest.skip('''Model is not available.'''       )
			def      lowerCamelCase    (self       )	->  Union[str, Any]:


							'''simple docstring'''



							snake_case_		:       List[str]           =    '''nvidia/megatron-bert-uncased-345m'''
							if "MYDIR" in os.environ:
											snake_case_		:       Dict           =    os.path.join(os.environ['''MYDIR''']   ,				__magic_name__       )
							snake_case_		:       str           =    MegatronBertModel.from_pretrained(__magic_name__       )
							model.to(__magic_name__       )
							model.half()
							snake_case_		:       Union[str, Any]           =    _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]       )
							with torch.no_grad():
											snake_case_		:       int           =    model(__magic_name__       )[0]
							snake_case_		:       Any           =    torch.Size((1, 9, 1024)       )
							self.assertEqual(output.shape   ,				__magic_name__       )

							snake_case_		:       List[str]           =    [-0.6_040, -0.2_517, -0.1_025, 0.3_420, -0.6_758, -0.0_017, -0.1_089, -0.1_990, 0.5_728]
							for ii in range(3       ):
											for jj in range(3       ):
															snake_case_		:       Optional[Any]           =    output[0, ii, jj]
															snake_case_		:       List[Any]           =    expected[3 * ii + jj]
															snake_case_		:       int           =    '''ii={} jj={} a={} b={}'''.format(__magic_name__   ,				__magic_name__   ,				__magic_name__   ,				__magic_name__       )
															self.assertTrue(math.isclose(__magic_name__   ,				__magic_name__   ,				rel_tol=__magic_name__   ,				abs_tol=__magic_name__       )   ,				msg=__magic_name__       )








from __future__ import annotations
class 		SCREAMING_SNAKE_CASE__	:

  '''simple docstring'''







  def __init__(      self		:			Optional[Any]       ,				lowerCamelCase		:			list[list[int]]							)						->	Any:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError(
             """Matrices must be formed from a list of zero or more lists containing at """
             """least one and the same number of values, each of which must be of type """
             """int or float."""							)
         if len(lowerCamelCase							) != 0:
                _UpperCAmelCase							=    len(rows[0]							)
                if cols == 0:
                       raise error
                for row in rows:
                       if len(lowerCamelCase							) != cols:
                              raise error
                       for value in row:
                              if not isinstance(lowerCamelCase       ,				(int, float)							):
                                     raise error
                _UpperCAmelCase							=    rows
         else:
                _UpperCAmelCase							=    []


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	list[list[int]]:


         """simple docstring"""
         return [[row[i] for row in self.rows] for i in range(len(self.rows[0]							)							)]


  @property
  def  lowerCamelCase       (      self		:			int							)						->	int:


         """simple docstring"""
         return len(self.rows							)


  @property
  def  lowerCamelCase       (      self		:			int							)						->	int:


         """simple docstring"""
         return len(self.rows[0]							)


  @property
  def  lowerCamelCase       (      self		:			Tuple							)						->	tuple[int, int]:


         """simple docstring"""
         return (self.num_rows, self.num_columns)


  @property
  def  lowerCamelCase       (      self		:			Tuple							)						->	bool:


         """simple docstring"""
         return self.order[0] == self.order[1]


  def  lowerCamelCase       (      self		:			Optional[int]							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [0 if column_num != row_num else 1 for column_num in range(self.num_rows							)]
             for row_num in range(self.num_rows							)
         ]
         return Matrix(lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	int:


         """simple docstring"""
         if not self.is_square:
                return 0
         if self.order == (0, 0):
                return 1
         if self.order == (1, 1):
                return int(self.rows[0][0]							)
         if self.order == (2, 2):
                return int(
                    (self.rows[0][0] * self.rows[1][1])
                    - (self.rows[0][1] * self.rows[1][0])							)
         else:
                return sum(
                    self.rows[0][column] * self.cofactors().rows[0][column]
                    for column in range(self.num_columns							)							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	bool:


         """simple docstring"""
         return bool(self.determinant()							)


  def  lowerCamelCase       (      self		:			Tuple       ,				lowerCamelCase		:			int       ,				lowerCamelCase		:			int							)						->	int:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [
                 self.rows[other_row][other_column]
                 for other_column in range(self.num_columns							)
                 if other_column != column
             ]
             for other_row in range(self.num_rows							)
             if other_row != row
         ]
         return Matrix(lowerCamelCase							).determinant()


  def  lowerCamelCase       (      self		:			int       ,				lowerCamelCase		:			int       ,				lowerCamelCase		:			int							)						->	int:


         """simple docstring"""
         if (row + column) % 2 == 0:
                return self.get_minor(lowerCamelCase       ,				lowerCamelCase							)
         return -1 * self.get_minor(lowerCamelCase       ,				lowerCamelCase							)


  def  lowerCamelCase       (      self		:			str							)						->	Matrix:


         """simple docstring"""
         return Matrix(
             [
                 [self.get_minor(lowerCamelCase       ,				lowerCamelCase							) for column in range(self.num_columns							)]
                 for row in range(self.num_rows							)
             ]							)


  def  lowerCamelCase       (      self		:			Dict							)						->	Matrix:


         """simple docstring"""
         return Matrix(
             [
                 [
                     self.minors().rows[row][column]
                     if (row + column) % 2 == 0
                     else self.minors().rows[row][column] * -1
                     for column in range(self.minors().num_columns							)
                 ]
                 for row in range(self.minors().num_rows							)
             ]							)


  def  lowerCamelCase       (      self		:			Optional[Any]							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    [
             [self.cofactors().rows[column][row] for column in range(self.num_columns							)]
             for row in range(self.num_rows							)
         ]
         return Matrix(lowerCamelCase							)


  def  lowerCamelCase       (      self		:			Any							)						->	Matrix:


         """simple docstring"""
         _UpperCAmelCase							=    self.determinant()
         if not determinant:
                raise TypeError("""Only matrices with a non-zero determinant have an inverse"""							)
         return self.adjugate() * (1 / determinant)


  def __repr__(      self		:			List[str]							)						->	str:


         """simple docstring"""
         return str(self.rows							)


  def __str__(      self		:			Optional[Any]							)						->	str:


         """simple docstring"""
         if self.num_rows == 0:
                return "[]"
         if self.num_rows == 1:
                return "[[" + ". ".join(str(self.rows[0]							)							) + "]]"
         return (
             "["
             + "\n ".join(
                 [
                     """[""" + """. """.join([str(lowerCamelCase							) for value in row]							) + """.]"""
                     for row in self.rows
                 ]							)
             + "]"
         )


  def  lowerCamelCase       (      self		:			Tuple       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			int | None = None							)						->	None:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError("""Row must be a list containing all ints and/or floats"""							)
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise type_error
         for value in row:
                if not isinstance(lowerCamelCase       ,				(int, float)							):
                       raise type_error
         if len(lowerCamelCase							) != self.num_columns:
                raise ValueError(
                    """Row must be equal in length to the other rows in the matrix"""							)
         if position is None:
                self.rows.append(lowerCamelCase							)
         else:
                _UpperCAmelCase							=    self.rows[0:position] + [row] + self.rows[position:]


  def  lowerCamelCase       (      self		:			List[Any]       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			int | None = None							)						->	None:


         """simple docstring"""
         _UpperCAmelCase							=    TypeError(
             """Column must be a list containing all ints and/or floats"""							)
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise type_error
         for value in column:
                if not isinstance(lowerCamelCase       ,				(int, float)							):
                       raise type_error
         if len(lowerCamelCase							) != self.num_rows:
                raise ValueError(
                    """Column must be equal in length to the other columns in the matrix"""							)
         if position is None:
                _UpperCAmelCase							=    [self.rows[i] + [column[i]] for i in range(self.num_rows							)]
         else:
                _UpperCAmelCase							=    [
                    self.rows[i][0:position] + [column[i]] + self.rows[i][position:]
                    for i in range(self.num_rows							)
                ]


  def __eq__(      self		:			Dict       ,				lowerCamelCase		:			object							)						->	bool:


         """simple docstring"""
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                return NotImplemented
         return self.rows == other.rows


  def __ne__(      self		:			List[str]       ,				lowerCamelCase		:			object							)						->	bool:


         """simple docstring"""
         return not self == other


  def __neg__(      self		:			Union[str, Any]							)						->	Matrix:


         """simple docstring"""
         return self * -1


  def __add__(      self		:			Any       ,				lowerCamelCase		:			Matrix							)						->	Matrix:


         """simple docstring"""
         if self.order != other.order:
                raise ValueError("""Addition requires matrices of the same order"""							)
         return Matrix(
             [
                 [self.rows[i][j] + other.rows[i][j] for j in range(self.num_columns							)]
                 for i in range(self.num_rows							)
             ]							)


  def __sub__(      self		:			Optional[Any]       ,				lowerCamelCase		:			Matrix							)						->	Matrix:


         """simple docstring"""
         if self.order != other.order:
                raise ValueError("""Subtraction requires matrices of the same order"""							)
         return Matrix(
             [
                 [self.rows[i][j] - other.rows[i][j] for j in range(self.num_columns							)]
                 for i in range(self.num_rows							)
             ]							)


  def __mul__(      self		:			List[Any]       ,				lowerCamelCase		:			Matrix | int | float							)						->	Matrix:


         """simple docstring"""
         if isinstance(lowerCamelCase       ,				(int, float)							):
                return Matrix(
                    [[int(element * other							) for element in row] for row in self.rows]							)
         elif isinstance(lowerCamelCase       ,				lowerCamelCase							):
                if self.num_columns != other.num_rows:
                       raise ValueError(
                           """The number of columns in the first matrix must """
                           """be equal to the number of rows in the second"""							)
                return Matrix(
                    [
                        [Matrix.dot_product(lowerCamelCase       ,				lowerCamelCase							) for column in other.columns()]
                        for row in self.rows
                    ]							)
         else:
                raise TypeError(
                    """A Matrix can only be multiplied by an int, float, or another matrix"""							)


  def __pow__(      self		:			Any       ,				lowerCamelCase		:			int							)						->	Matrix:


         """simple docstring"""
         if not isinstance(lowerCamelCase       ,				lowerCamelCase							):
                raise TypeError("""A Matrix can only be raised to the power of an int"""							)
         if not self.is_square:
                raise ValueError("""Only square matrices can be raised to a power"""							)
         if other == 0:
                return self.identity()
         if other < 0:
                if self.is_invertable():
                       return self.inverse() ** (-other)
                raise ValueError(
                    """Only invertable matrices can be raised to a negative power"""							)
         _UpperCAmelCase							=    self
         for _ in range(other - 1							):
                result *= self
         return result





  @classmethod
  def  lowerCamelCase       (      cls		:			Tuple       ,				lowerCamelCase		:			list[int]       ,				lowerCamelCase		:			list[int]							)						->	int:


         """simple docstring"""
         return sum(row[i] * column[i] for i in range(len(lowerCamelCase							)							)							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()